尝试通过应用程序向云存储进行身份验证时,“权限不足”
我即将放弃此操作,因为我找不到我做错了什么。尝试通过应用程序向云存储进行身份验证时,“权限不足”
我有一个云存储桶与我公司的账单数据(由Google编写的json文件对象),我应该处理成电子表格。
由于没有对的oauth2应用脚本API,我使用谷歌与键“1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF”提供的自定义的OAuth2库,并示出在这个例子中的服务帐户已经设置了身份验证请求: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())
}
不管是什么我似乎尝试,请求总是返回“403许可不足”。尽管(DwD,存储管理员,项目所有者)服务帐户具有激活的所有必要角色和权限。当我使用gcloud提供的相同凭据进行身份验证,然后使用gsutils浏览存储桶时,我可以看到列表。这使我相信,我仍然错误地请求身份验证令牌。我尝试使用curl生成的令牌并获得相同的权限不足响应。
在请求令牌时,我做错了什么? 请求的范围太窄吗?
请求的范围是否过窄?
,他们是。你可以找到OAuth scopes for Google's Cloud Storage API如下(你不需要使用所有这些,挑选那些最适合你的使用情况,在列表中的第1和第5范围应足够):
- https://www.googleapis.com/auth/cloud-platform
-
- https://www.googleapis.com/auth/devstorage.full_control
- https://www.googleapis.com/auth/devstorage.read_only
- https://www.googleapis.com/auth/devstorage.read_write
今后,您可以在以下链接中找到所需的任何Google API所需的OAuth范围: https://developers.google.com/identity/protocols/googlescopes
非常感谢!我不得不添加云平台。只读范围,现在一切正常。我们在开始的时候确实设置了这个范围,但显然缺少了其他的东西,因此将范围缩小了。 –