401从ASP.NET在Google域中使用Google驱动器API时出错
我目前正在尝试向我们公司的ASP.NET网站添加功能,允许最终用户将网站生成的文件上传到同一公司内他们自己的Google驱动器中 该公司拥有自己的谷歌域名,其中包括邮件、硬盘、联系人、日历和几乎所有其他内容。它使用SAML通过我们的Active Directory设置进行身份验证,该设置链接到Google 需要注意的是,此代码适用于我的@gmail.com帐户。使用公司的Google应用程序域帐户,我得到底部显示的无效凭据错误。我已经和我们的谷歌管理员谈过了,他声称我的帐户在驱动API访问方面没有任何限制。请记住,API ID/Secret是使用我的公司帐户而不是Gmail帐户创建的401从ASP.NET在Google域中使用Google驱动器API时出错,asp.net,c#-4.0,oauth-2.0,google-drive-api,Asp.net,C# 4.0,Oauth 2.0,Google Drive Api,我目前正在尝试向我们公司的ASP.NET网站添加功能,允许最终用户将网站生成的文件上传到同一公司内他们自己的Google驱动器中 该公司拥有自己的谷歌域名,其中包括邮件、硬盘、联系人、日历和几乎所有其他内容。它使用SAML通过我们的Active Directory设置进行身份验证,该设置链接到Google 需要注意的是,此代码适用于我的@gmail.com帐户。使用公司的Google应用程序域帐户,我得到底部显示的无效凭据错误。我已经和我们的谷歌管理员谈过了,他声称我的帐户在驱动API访问方面没
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();
}
}
}
问题发生在请求上。上载行出现以下异常
异常详细信息:系统。异常:无效凭据
源错误:第85行:请求。上载
源文件:C:\TMGGoogle\TMGGoogle\TMGGoogle\GoogleAuth.aspx.cs
电话号码:85
堆栈跟踪:
[例外:无效凭据]
Google.api.Upload.ResumableUpload`1.Upload+722
GoogleTesting.GoogleAuth.UploadFile
非常感谢您的帮助。谢谢。嗯,我找到了问题的根本原因。事实证明,我们公司的谷歌应用程序帐户关闭了Docs API访问 我们的谷歌应用程序管理员已经打开了它,这已经解决了这个问题