我目前正在尝试向我们公司的ASP.NET
网站添加功能,该功能将允许最终用户将网站生成的文件上传到他们自己的Google Drive
(在同一公司内)。
该公司拥有自己的 Google 域,其中包括邮件、云端硬盘、联系人、日历(以及几乎所有其他内容)。它使用 SAML 对我们的 Active Directory 设置进行身份验证,该设置链接到 Google。
应该注意的是,此代码适用于我的@gmail.com
帐户。使用公司 Google App Domain 帐户,我收到无效凭据错误(显示在底部)。我已与我们的 Google 管理员交谈过,他们声称我的帐户在 Drive API 访问方面没有任何限制。另请记住,API ID/Secret 是使用我的公司帐户而不是 Gmail 帐户创建的。
using System;
using System.IO;
using System.Text;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Util;
namespace GoogleTesting
{
public partial class GoogleAuth : System.Web.UI.Page
{
private static readonly string CLIENTID = "xxxxxxxxx.apps.googleusercontent.com";
private static readonly string CLIENTSECRET = "xxxxxxxxxxxxxxxxxxxxxx";
private static readonly string APIKEY = "xxxxxxxxxxx";
private static readonly string REDIRECTURI = http://localhost:61111/default.aspx";
private static readonly string[] SCOPES = new[] { DriveService.Scopes.Drive.GetStringValue() };
private static DriveService driveService;
private static OAuth2Authenticator<WebServerClient> authenticator;
private static IAuthorizationState _state;
private IAuthorizationState AuthState
{
get
{
return _state ?? HttpContext.Current.Session["GoogleAuthState"] as IAuthorizationState;
}
}
private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
{
var provider = new WebServerClient(GoogleAuthenticationServer.Description, CLIENTID, CLIENTSECRET);
var authenticator = new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization);
return authenticator;
}
private IAuthorizationState GetAuthorization(WebServerClient client)
{
IAuthorizationState state = AuthState;
if (state != null)
{
if (state.AccessTokenExpirationUtc.Value.CompareTo(DateTime.Now.ToUniversalTime()) > 0)
return state;
else
state = null;
}
state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
{
if (state.RefreshToken == null)
state.RefreshToken = "";
HttpContext.Current.Session["GoogleAuthState"] = _state = state;
return state;
}
client.RequestUserAuthorization(SCOPES, "", HttpContext.Current.Request.Url);
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
if(authenticator == null)
authenticator = CreateAuthenticator();
if (driveService == null)
{
driveService = new DriveService(authenticator);
driveService.Key = APIKEY;
}
//We should now be authenticated and ready to use Drive API.
UploadFile();
}
private void UploadFile()
{
Google.Apis.Drive.v2.Data.File newFile = new Google.Apis.Drive.v2.Data.File { Title = "Test File", MimeType = "text/plain" };
byte[] byteArray = Encoding.ASCII.GetBytes("Body of the document.");
MemoryStream stream = new MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = driveService.Files.Insert(newFile, stream, newFile.MimeType);
request.Convert = true;
request.Upload();
}
}
}
问题出现在“request.Upload”行,但有以下异常。
异常详细信息:System.Exception:无效凭据
源错误:第 85 行:request.Upload();
源文件:C:\TMGGoogle\TMGGoogle\TMGGoogle\GoogleAuth.aspx.cs
行:85堆栈跟踪:
[例外:无效凭据]
Google.Apis.Upload.ResumableUpload`1.Upload()
+722 GoogleTesting.GoogleAuth.UploadFile()
任何帮助深表感谢。谢谢。