"Insufficient Permission" 尝试通过应用程序脚本对云存储进行身份验证时

"Insufficient Permission" when trying to authenticate to cloud-storage via apps-script

我快要放弃了,因为我找不到我做错了什么。

我有一个云存储桶,其中包含我们公司的账单数据(json 由 google 编写的文件对象),我应该将其处理成电子表格。

由于没有用于 oauth2 的应用程序脚本 API,我正在使用 google 提供的自定义 OAuth2 库,密钥为“1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF”,并已设置身份验证请求,如此所示服务帐户示例:https://github.com/googlesamples/apps-script-oauth2/blob/master/samples/GoogleServiceAccount.gs

正在创建令牌并将其放入脚本 属性 存储区,我可以在其中查看它。到目前为止一切顺利。

我有这个请求令牌的代码,然后我试图在函数 "getFilesList()":

中列出存储桶的内容
function getService() {
return OAuth2.createService('CloudStoreGrab-Service')
  .setTokenUrl('https://accounts.google.com/o/oauth2/token')

  .setPrivateKey(creds_private_key)
  .setIssuer(creds_client_email)
  .setSubject(creds_user_email)
  .setPropertyStore(PropertiesService.getScriptProperties())
  .setScope(['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/script.external_request','https://www.googleapis.com/auth/script.storage','https://www.googleapis.com/auth/spreadsheets']);
}

function getFilesList() {
var service = getService();
service.reset();
if (service.hasAccess()) {
    var url = 'https://www.googleapis.com/storage/v1/b/'+bucket+'/o'; 
    var response = UrlFetchApp.fetch(url, {
        method: "GET",
        muteHttpExceptions: true,        
        headers: {
            Authorization: 'Bearer ' + service.getAccessToken()               
        }
    });
}
Logger.log("Response:", response.getContentText())
}

无论我怎么尝试,请求总是returns“403 权限不足”。服务帐户已激活所有必要的角色和权限(DwD、存储管理员、项目所有者)。当我使用来自 gcloud 的相同凭据进行身份验证,然后使用 gsutils 浏览存储桶时,我可以看到列表。这让我相信,我仍然在错误地请求身份验证令牌。我尝试将生成的令牌与 curl 一起使用,但得到了相同的权限不足响应。

我在请求令牌时做错了什么? 请求的范围是否太窄?

Are the requested scopes too narrow?

他们是。您可以找到下面列出的 OAuth scopes for Google's Cloud Storage API(您不需要全部使用它们,选择最适合您的用例的那些,列表中的第 1 和第 5 个范围应该足够了):

将来,您可以在以下 link 中找到任何 Google API 所需的 OAuth 范围: https://developers.google.com/identity/protocols/googlescopes