2016-08-12 149 views
2

我試圖從服務帳戶創建的文檔所有權轉讓給使用下面的代碼誰所在的同一個谷歌Apps帳戶中的其他用戶,但我發現了以下錯誤谷歌雲端硬盤API - 從服務帳戶所有權轉移

資源體包含不可直接寫入的字段。 [403]誤差 [內容[資源體包括不直接可寫字段]地址[ - ]原因[fieldNotWritable]域[全球]]

 var service = GetService(); 

     try 
     { 
      var permission = GetPermission(fileId, email); 
      permission.Role = "owner"; 

      var updatePermission = service.Permissions.Update(permission, fileId, permission.Id); 
      updatePermission.TransferOwnership = true; 
      return updatePermission.Execute(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("An error occurred: " + e.Message); 
     } 
     return null; 

註釋掉// permission.Role =「所有者」;返回以下錯誤

當權限角色爲「所有者」時,必須啓用transferOwnership參數。 [403]錯誤[消息[權限角色爲'所有者'時必須啓用transferOwnership參數。]位置[transferOwnership - 參數]原因[禁止]域[全局]]

分配任何其他權限都可以正常工作。因此,這是服務帳戶無法將所有權轉讓給任何其他未使用@ gserviceaccount.com電子郵件地址(即[email protected]> [email protected])的帳戶的限制。 ?

[email protected]電子郵件地址已創建並在Google Apps中進行管理。

在這種情況下,它是不可實現的,任何指向下一步的指針?我們需要多個用戶有能力通過API臨時創建文檔並分配權限並實時傳輸所有權。

感謝

+0

嘗試使用補丁而不是更新? https://developers.google.com/drive/v2/reference/permissions/patch – DaImTo

+0

謝謝@DalmTo但我使用的是V3 API,更新功能是V2中的舊補丁功能[遷移到Google Drive API v3]( https://developers.google.com/drive/v3/web/migration)。 – timkly

+0

好的是一個猜測很難判斷它是否是你的代碼的v2或v3。修補工作在V2中。權限是谷歌驅動器的一個痛苦。 – DaImTo

回答

0

我找到了答案,併發布給遇到此問題的任何其他人。

  1. 您不能使用Google推薦的'服務帳戶密鑰JSON文件'。
  2. 您需要使用p.12證書文件進行身份驗證。
  3. 創建用於模擬帳戶的驅動器服務的代碼如下所示。

    public DriveService GetService(string certificatePath, string certificatePassword, string googleAppsEmailAccount, string emailAccountToMimic, bool allowWrite = true) 
    { 
        var certificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.Exportable); 
    
        var credential = new ServiceAccountCredential(
         new ServiceAccountCredential.Initializer(googleAppsEmailAccount) 
         { 
          Scopes = new[] { allowWrite ? DriveService.Scope.Drive : DriveService.Scope.DriveReadonly }, 
          User = emailAccountToMimic 
         }.FromCertificate(certificate)); 
    
        // Create the service. 
        return new DriveService(new BaseClientService.Initializer 
        { 
         HttpClientInitializer = credential, 
         ApplicationName = ApplicationName 
        }); 
    } 
    
  4. 您需要按照上市here委託域範圍內的機關服務帳戶的步驟。

  5. 完成第4步後需要5到10分鐘。
  6. 您現在可以在'emailAccountToMimic'用戶下創建文檔,並在創建過程中將其設置爲所有者。
+0

僅供參考 - 事實證明,您需要使用emailAccountToMimic帳戶創建文檔,然後將「Writer」權限分配給服務帳戶的文檔,然後寫入文檔。 – timkly

0

我不認爲這是可能從非ServiceAccount的所有權轉讓給ServiceAccount,反之亦然。

如果你這樣做交互,你會得到下面的錯誤:

enter image description here

通常情況下,該文件可以創建和由用戶擁有並可以使用自己的憑據來完成過戶。如果您的服務帳戶已正確授予域範圍的委派,您也可以選擇模擬爲所有者。

+0

謝謝@ some1。實際上,我嘗試將ServiceAccount中的權限分配給非ServiceAccount,並且我已經爲該服務帳戶設置了域範圍的委派併爲[如此解釋]分配了權限(https://developers.google.com/identity/)協議/ OAuth2ServiceAccount),但我使用的是服務帳戶密鑰JSON文件(由Google推薦),它具有不同於初始者的OAuth 2.0身份驗證,它依賴於憑證而不是...但它也允許在使用ServiceAccountCredential時帳戶模仿。初始化方法。 – timkly

相關問題