Python调用GCS API遇403错误:无storage.objects.get访问权限
Python下载GCS销售报告遇403权限问题
我尝试通过Python代码下载Google Cloud Storage(GCS)中的销售报告,代码如下:
import json from httplib2 import Http from oauth2client.client import SignedJwtAssertionCredentials from apiclient.discovery import build # Change these variables to fit your case client_email = 'nameofserviceaccount.iam.gserviceaccount.com' json_file = '/Users/xxx/Downloads/key.json' cloud_storage_bucket = 'pubsite_prod_rev_XXXXXXX' report_to_download = 'earnings/earnings_XXXXX.zip' private_key = json.loads(open(json_file).read())['private_key'] credentials = SignedJwtAssertionCredentials(client_email, private_key,'https://www.googleapis.com/auth/devstorage.read_only') storage = build('storage', 'v1', http=credentials.authorize(Http())) try: response = storage.objects().get(bucket=cloud_storage_bucket, object=report_to_download).execute() print(response) except Exception as e: print(f"Error: {e}")
执行后出现如下403错误:
<HttpError 403 when requesting https://storage.googleapis.com/storage/v1/b/pubsite_prod_rev_XXXXXXX/o/earnings%2Fearnings_XXXXXXX.zip?alt=json returned "nameofserviceaccount.iam.gserviceaccount.com does not have storage.objects.get access to the Google Cloud Storage object. Permission 'storage.objects.get' denied on resource (or it may not exist).". Details: "[{'message': "nameofserviceaccount.iam.gserviceaccount.com does not have storage.objects.get access to the Google Cloud Storage object. Permission 'storage.objects.get' denied on resource (or it may not exist).", 'domain': 'global', 'reason': 'forbidden'}]">
已排查的情况:
- 服务账号已配置Storage Admin和Storage Object Admin权限
- 尝试删除并重新创建服务账号,问题依旧
- 尝试修改应用内商品描述,问题未解决
- Cloud Storage API已启用
- 尝试移除并重新添加Storage Admin角色
- 单独为服务账号配置Storage Object Viewer角色
此前在Google Play Console设置中可正常访问API,自Google更新API访问配置方式后出现该问题。
内容的提问来源于stack exchange,提问作者greenmonkeys
相关产品推荐
相关产品推荐

