从ASP.NET中使用Google Domain中的Google Drive API时出错401

时间:2022-08-22 10:58:20

I am currently trying to add functionality to our company's ASP.NET website which will allow end users to upload files generated from the website into their own Google Drive (within the same company).

我目前正在尝试为我们公司的ASP.NET网站添加功能,这将允许最终用户将从网站生成的文件上传到他们自己的Google云端硬盘(在同一家公司内)。

The company has it's own Google Domain which includes Mail, Drive, Contacts, Calendar (and pretty much everything else). It uses SAML to authenticate with our Active Directory set up which links in to Google.

该公司拥有自己的Google Domain,其中包括Mail,Dri​​ve,Contacts,Calendar(以及其他所有内容)。它使用SAML通过我们的Active Directory设置进行身份验证,该设置链接到Google。

It should be noted that this code works OK with my @gmail.com account. With the companies Google App Domain account, I get the invalid credentials error (shown at the bottom). I have spoken with our Google Admin who claims that I have no restrictions on my account with regards to Drive API access. Please also bear in mind that the API ID/Secret's were created with my company account rather than a Gmail account.

应该注意的是,此代码可以与我的@ 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(); 
        }
    }
}

The problem occurs on the "request.Upload" line with the following exception.

问题发生在“request.Upload”行,出现以下异常。

Exception Details: System.Exception: Invalid Credentials

异常详细信息:System.Exception:无效的凭据

Source Error: Line 85: request.Upload();

源错误:第85行:request.Upload();

Source File: C:\TMGGoogle\TMGGoogle\TMGGoogle\GoogleAuth.aspx.cs
Line: 85

源文件:C:\ TMGGoogle \ TMGGoogle \ TMGGoogle \ GoogleAuth.aspx.cs行:85

Stack Trace:

[Exception: Invalid Credentials]
Google.Apis.Upload.ResumableUpload`1.Upload() +722
GoogleTesting.GoogleAuth.UploadFile()

[例外:凭据无效] Google.Apis.Upload.ResumableUpload`1.Upload()+ 722 GoogleTesting.GoogleAuth.UploadFile()

Any help is much appreciated. Thanks.

任何帮助深表感谢。谢谢。

1 个解决方案

#1


0  

Well, I figured out the root cause of the problem. It turns out that our company's Google Apps account had Docs API access turned off.

好吧,我找出了问题的根本原因。事实证明,我们公司的Google Apps帐户已关闭了Google文档API访问权限。

Our Google Apps Administrator has since turned it on and this has resolved the issue.

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

#1


0  

Well, I figured out the root cause of the problem. It turns out that our company's Google Apps account had Docs API access turned off.

好吧,我找出了问题的根本原因。事实证明,我们公司的Google Apps帐户已关闭了Google文档API访问权限。

Our Google Apps Administrator has since turned it on and this has resolved the issue.

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