【问题标题】:401 Error using Google Drive API within Google Domain from ASP.NET在 ASP.NET 的 Google 域中使用 Google Drive API 时出现 401 错误
【发布时间】:2012-09-14 10:08:30
【问题描述】:

我目前正在尝试向我们公司的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()

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: asp.net c#-4.0 oauth-2.0 google-drive-api


    【解决方案1】:

    好吧,我找到了问题的根本原因。事实证明,我们公司的 Google Apps 帐户关闭了 Docs API 访问。

    我们的 Google Apps 管理员已将其打开,问题已解决。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2016-10-06
      相关资源
      最近更新 更多