【问题标题】:Scan Drive Contents of Google App Domain Users扫描 Google App 域用户的云端硬盘内容
【发布时间】:2016-02-10 05:57:44
【问题描述】:
【问题讨论】:
标签:
google-apps-script
google-api
【解决方案1】:
是的,正如您所说,您需要使用服务帐户。当您使用OAuth2 library 的服务帐户时,您可以设置“主题”(您要模拟的用户帐户)。图书馆有一个service account sample。
这是一个稍作修改的 OAuth2.getService() 示例,它将用户帐户电子邮件作为参数。当您遍历您的用户列表时,您将在每个 https://developers.google.com/drive/v3/reference/files/list Drive API 调用之前运行它。
/**
* Configures the service.
*/
function getService(userEmail) {
return 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');
}