2017-04-23 80 views
0

我有一個服務帳戶設置與域寬委派。我想通過一次通話在所有用戶中搜索文件。獲取所有用戶的所有云端硬盤文件

我在Google Apps腳本中使用Drive API和OAuth2項目來處理身份驗證。看起來你總是必須指定一個用戶在進行調用時模擬(下面的代碼)。當我刪除setSubject方法時,我沒有得到任何結果。

有沒有辦法做到這一點?

OAuth2.createService('GoogleDrive:' + userEmail) 
    // Set the endpoint URL. 
    .setTokenUrl('https://accounts.google.com/o/oauth2/token') 

    // Set the private key and issuer. 
    .setPrivateKey(PRIVATE_KEY) 
    .setIssuer(CLIENT_EMAIL) 

    // Set the name of the user to impersonate. This will only work for 
    // Google Apps for Work/EDU accounts whose admin has setup domain-wide 
    // delegation: 
    // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority 
    .setSubject(userEmail) 

    // Set the property store where authorized tokens should be persisted. 
    .setPropertyStore(PropertiesService.getScriptProperties()) 

    // Set the scope. This must match one of the scopes configured during the 
    // setup of domain-wide delegation. 
    .setScope('https://www.googleapis.com/auth/drive'); 

感謝

+0

你是正確使用服務帳戶設置與域寬委派。如此[線程](http://stackoverflow.com/a/13732646/5832311)中所述,您應該使用Google Drive API並使用服務帳戶執行[Google Apps域範圍授權](https:/ /developers.google.com/drive/delegation)。 – abielita

+0

根據此[文檔](https://developers.google.com/apps-script/reference/drive/),您需要返回['FileIterator'](https://developers.google.com/apps -script/reference/drive/file-iterator),它允許腳本遍歷潛在的大量文件集合。以下是一個示例[代碼](https://productforums.google.com/forum/#!topic/docs/7ZQgzbcSX68)。 – abielita

+0

@abielita對不起,我不夠具體。我已經使用帶有服務帳戶的Drive API。我在上面的文章中留了更多細節。謝謝 – jadent

回答

0

你可以嘗試這可以從應用程序腳本通過Advanced Drive Service訪問的Drive API。它與DriveApp相比具有其他功能,並允許您使用搜索查詢。您也可以使用API Explorer來測試您的請求。例如,將查詢字符串從Advanced Drive Service頁複製到API Explorerq字段中。該查詢字符串是"root" in parents and trashed = false and mimeType = "application/vnd.google-apps.folder"。如果你點擊執行,你會看到結果。查詢字符串的語法解釋爲here

+0

謝謝@SpiderPig。對不起,我不夠具體。我使用Drive API更新了更多信息的帖子,但如果我不模仿用戶,則無法返回結果。所以想知道如何在不模仿用戶的情況下撥打電話,以便在1次通話中搜索所有文件。謝謝 – jadent

相關問題