2012-07-24 31 views
7

新的Drive SDK對於經過身份驗證的用戶非常有用。是否可以使用Drive SDK使用Google Apps管理訪問來模擬其他域用戶?Google Apps管理員可以使用雲端硬盤SDK管理用戶文件嗎?

doclist API可以做到這一點,但它不可能用這個工具管理和複製文件(pdf,jpg)。

我使用的Java與此代碼:

credential_origine = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) 
        .setJsonFactory(JSON_FACTORY) 
        .setServiceAccountId("[email from console api]") 
        .setServiceAccountScopes(DriveScopes.DRIVE) 
        .setServiceAccountUser("[email protected]") 
        .setServiceAccountPrivateKeyFromP12File(new File("key.p12")).build(); 

但我得到一個錯誤,當我嘗試了[email protected]檢索數據。如果我對.setServiceAccountUser("[email protected]")發表評論,該代碼對於我用於創建密鑰的同一帳戶非常有用。

在舊的DOCList API中,我們通過請求的URL模擬了另一個用戶。它是否類似?

+0

你會得到什麼錯誤?請確保按照[此處](http://support.google.com/a/bin/answer.py?hl=zh_CN&answer=162106)中所述,從您域的cPanel中的API控制檯添加生成的客戶端ID。 – Alain 2012-07-26 15:47:38

+0

嗨,Alain,現在工作正常,可能是我錯了ServiceAccountUser。當我嘗試用updatePermission和patchPermission更改文件的所有者時,我收到500內部服務器錯誤...也許是因爲文件必須擁有一個所有者..我會再試一次,感謝您的幫助 – Ridgh 2012-07-26 18:20:20

+0

@Ridgh can you give我有關使用管理員SDK訪問用戶文檔的一些想法。目前我正在處理用戶文檔 – 2014-09-18 13:03:16

回答

4

您可以使用Service Accounts並指定用戶在構建斷言聲明時模仿。

在您的API項目(從APIs Console)中創建服務帳戶密鑰後,您必須將此項目添加到cPanel中授權的第三方應用程序列表中。有關詳情,請參見here。 「客戶端ID」你需要使用的是一個綁定到服務帳戶鍵,看起來像<APP_ID>-<OTHER_KEY>.apps.googleusercontent.com

由於要管理其他用戶的文件,你將不得不授權驅動範圍廣:https://www.googleapis.com/auth/drive

我們的大部分客戶端庫負責爲開發人員提取索賠代。如果您可以指定您打算使用哪種語言,我可以通過提供一段代碼來幫助您開始更新此答案。

+0

有沒有一個Ruby庫來實現這一目標? – tbeseda 2012-11-12 22:15:41

+0

用於Ruby的Google客戶端庫確實支持服務帳戶:https://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts – Alain 2013-04-03 21:25:31

0

升級:

代碼:

com.google.api.client.googleapis.auth.oauth2.GoogleCredential.Builder credential_origine_builder = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT) 
        .setJsonFactory(JSON_FACTORY) 
        .setServiceAccountId("[[]]") 
        .setServiceAccountScopes(DriveScopes.DRIVE) 
        .setServiceAccountPrivateKeyFromP12File(new File("cfg/file.p12")); 

credential_origine_builder.setServiceAccountUser("[email protected]"); 

工作正常。

如果我們改變了.setServiceAccountScopes(DriveScopes.DRIVE)

.setServiceAccountScopes(DriveScopes.DRIVE,Oauth2Scopes.USERINFO_EMAIL,Oauth2Scopes.USERINFO_PROFILE) 

用於檢索用戶的它似乎無法與.setServiceAccountUser("[email protected]");

使用3和範圍相適應的姓名和身份證工作正常只有對用戶「老闆「的鍵...保持建設

+0

您是否在配置了oAuth訪問權限的cPanel?從Google cPanel幫助:對於每個客戶端,您可以指定多個API,以逗號分隔。例如,要允許訪問聯繫人和文檔列表API:「http://www.google.com/m8/feeds/,http://docs.google.com/feeds/」 – David 2012-07-27 16:17:53

+0

David,在我的代碼中我確定在Google Api控制檯上添加了範圍,我沒有看到這個範圍,我錯了嗎?我使用了組設置API以及正確配置API和oauth 2.0 web流程。我的問題是關於doclist api +驅動器api和服務帳戶Oauth流...我希望只使用驅動器sdk,但它不可能管理權限(電子郵件字段不會返回隱私)。 – Ridgh 2012-07-29 17:19:20

0

好的這裏是完整的代碼在Dartlang列出模擬用戶@ YOUR_DOMAIN用戶文件與JSON密鑰

要運行它,你將需要通過Google Developer Console 生成JSON鍵(而不是P12)(以項目的上下文:蜜蜂&權威性 - >憑證) generate JSON key

飛鏢項目在pubspec.yaml依賴關係:googleapis googleapis_auth異步)

import 'package:googleapis/drive/v2.dart'; 
import 'package:googleapis_auth/auth_io.dart'; 
import 'package:async/async.dart'; 

final _credentials = new ServiceAccountCredentials.fromJson(r''' 
{ 
    "private_key_id": "*PRIVATE_KEY_ID*", 
    "private_key": "*PRIVATE_KEY_CONTENT*", 
    "client_email": "*[email protected]*", 
    "client_id": "*SOMETHING.apps.googleusercontent.co*m", 
    "type": "service_account" 
} 
''', impersonatedUser: "*[email protected]_DOMAIN*"); //here you type user 

const _SCOPES = const [DriveApi.DriveScope]; 

main() async { 
    var http_client = await clientViaServiceAccount(_credentials, _SCOPES); 
    var drive = new DriveApi(http_client); 
    var docs = await drive.files.list(maxResults: 10); 

    for (var itm in docs.items) { 
    print(itm.title); //list docs titles 
    } 
} 
相關問題