2012-09-14 60 views
1

我目前正在嘗試向我們公司的ASP.NET網站添加功能,該功能允許最終用戶將網站生成的文件上傳到自己的Google Drive(在同一公司內) 。401在Google域中使用Google Drive API時出錯

該公司擁有自己的Google Domain,其中包括郵件,雲端硬盤,聯繫人,日曆(以及其他所有內容)。它使用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」行。

Exception Details: System.Exception: Invalid Credentials

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

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

Stack Trace:

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

任何幫助,非常感謝。謝謝。

回答

0

嗯,我想出了問題的根源。事實證明,我們公司的Google Apps帳戶已關閉Google文檔API訪問權限。

我們的Google Apps管理員已將其開啓,並解決了此問題。