繁体   English   中英

带有 google-api-client-php 和 JSON 凭据的 Google Cloud Storage 但一段时间后出现 403 错误

[英]Google Cloud Storage with google-api-client-php and JSON credential but got 403 error after awhile

我正在尝试通过带有 JSON 凭据的 google-api-client-php SDK 使用 Google Cloud Storage 服务,并应用以下示例代码:

putenv("GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);

$storage = new Google_Service_Storage($client);
$ret = $storage->buckets->listBuckets("project-id");

但是,在我创建凭据的前几个小时后,这些代码没问题,但几个小时后,我一直收到“权限被拒绝”错误,其中包含相同的代码/凭据文件。

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

我不知道这是代码级别还是服务级别问题...

确保您的服务帐号在 GCP 中拥有正确的权限:

在控制台中,转到:

Api Manager > Credentials > Manage Service Accounts > Permissions > Add Member

在弹出屏幕上,粘贴您的服务帐户的电子邮件地址并授予其“可以编辑”访问权限。 那应该这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM