【发布时间】:2017-09-19 23:15:32
【问题描述】:
我有一个使用域范围委派的服务帐户设置。我想通过一次调用搜索所有用户的文件。
我在 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');
谢谢
【问题讨论】:
-
您可以将服务帐户设置与域范围委派一起使用。正如thread 中所述,您应该使用 Google Drive API 并使用服务帐户来执行Google Apps domain-wide delegation of authority。
-
基于此documentation,您需要返回一个
FileIterator,它允许脚本遍历可能很大的文件集合。这是一个示例code。 -
@abielita 抱歉,我不够具体。我已经通过服务帐户使用 Drive API。我在上面的帖子中留下了更多细节。谢谢