0

我試圖從Windows應用程序或服務使用Google驅動器。無法在Google驅動器中使用服務帳戶進行授權С#/。NET

我不需要Web應用程序,MVC等等。

系統必須自動工作,沒有任何用戶入侵,所以我創建了服務帳戶,並獲得證書和電子郵件。

,當然,下載的最後一個客戶端庫 - http://code.google.com/p/google-api-dotnet-client/

我建立服務對象作爲文檔:

 private const string SERVICE_ACCOUNT_EMAIL = "[email protected]"; 
    private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"xxx-privatekey.p12"; 

    /// <summary> 
    /// Build a Drive service object authorized with the service account. 
    /// </summary> 
    /// <returns>Drive service object.</returns> 
    static DriveService BuildService () 
    { 
     X509Certificate2 certificate = new X509Certificate2 (SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", 
      X509KeyStorageFlags.Exportable); 

     var provider = new AssertionFlowClient (GoogleAuthenticationServer.Description, certificate) 
     { 
      ServiceAccountId = SERVICE_ACCOUNT_EMAIL, 
      Scope = DriveService.Scopes.Drive.GetStringValue (), 
      ServiceAccountUser = "[email protected]", 
     }; 
     var auth = new OAuth2Authenticator<AssertionFlowClient> (provider, AssertionFlowClient.GetState); 
     return new DriveService (auth); 
    } // BuildService 

,然後我這樣做:

service = BuildService (); 
FilesResource.InsertMediaUpload request = service.Files.Insert (body, stream, mimeType); 
request.Upload (); 
File file = request.ResponseBody; 

我上傳方法中的例外 - 未知對象標識符(OID)。 我找到了引發此異常的命令:

String signature = UnpaddedUrlSafeBase64Encode(Key.SignData(Encoding.ASCII.GetBytes(assertion.ToString()),「SHA256」));

這在一個Google.Apis.Authentication.OAuth2.dll,文件AssertionflowClient.cs中,方法GenerateMessage。

但Google證書只有SHA1算法。這從證書信息窗口中是顯而易見的:X509Certificate2UI.DisplayCertificate(certificate);或從窗口。我試過使用SHA1: String signature = UnpaddedUrlSafeBase64Encode(Key.SignData(Encoding.ASCII.GetBytes(assertion.ToString()),「SHA1」));

之後,此異常消失,但我得到HTTP錯誤400(無效的請求)。 請幫助我。如何使用服務帳戶與Google驅動器配合使用?

謝謝。

PS 對不起,我可能不好英語,我是俄羅斯人。

回答

0

您是否按照OAuth2 service account頁面的說明操作並生成新密鑰? (InsertRequest Insert(Google.Apis.Drive.v1.Data.File body)) - 讓我知道如果其他方法不適用於您,您可以嘗試其他服務請求,可以說沒有媒體上傳插入請求(InsertRequest插入(Google.Apis.Drive.v1.Data.File正文)以及

,並注意在service account documentation它說:「服務帳戶依靠RSA SHA256算法和JWT令牌格式」這是支持的算法,而不是SHA1

+0

在我的情況下,當我嘗試使用BuildService()獲取文件時,它不返回任何數據。請幫助我。 – 2013-07-21 18:17:55

1

在你的代碼中刪除

ServiceAccountUser = "[email protected]", 

或 轉到谷歌應用程序域的管理控制檯並授權您的服務帳戶。

然後它工作。

+0

在我的情況下,當我嘗試使用BuildService()獲取文件時,它不返回任何數據。請幫助我。 – 2013-07-21 18:17:34

+0

這些文件是否與服務帳戶共享?此服務帳戶機制主要針對Google企業應用。如果您在個人雲端硬盤上使用這些API,則可能必須明確將這些文件共享到服務帳戶。 – 2013-08-13 09:48:10

相關問題