首页 文章

google.api_core.exceptions.Forbidden:403权限丢失或不足

提问于
浏览
1

提交了类似的问题,但没有一个解决方案有效 .

尝试从Google Cloud doc执行this tutorial时,尝试访问数据存储区时出现以下错误:

google.api_core.exceptions.Forbidden: 403 Missing or insufficient 
permissions.

执行文件可以找到here .

我确实执行了以下命令:

gcloud auth application-default login
export GOOGLE_APPLICATION_CREDENTIALS="file.json"

请注意,我正在本地计算机上执行该文件 . 目标是直接从Google Engine应用程序对数据存储区执行读/写操作 .

2 回答

  • 0

    您尝试使用两种不同形式的身份验证,我不建议这样做 .

    如果您希望本地应用程序临时使用您自己的用户凭据进行API访问,则来自Google的documentation,"gcloud auth application-default login" .

    当您使用"export GOOGLE_APPLICATION_CREDENTIALS='file.json'"时,根据Google的documentation,您要将环境变量设置为"file.json" . 这意味着您需要创建服务帐户,为服务帐户分配适当的权限,创建/下载密钥(在本例中为"file.json"),然后环境变量将在您的代码执行时生效 .

    由于您刚开始使用,我建议您开始使用Google Cloud Console中提供的Cloud Shell并使用拥有Google项目完整所有者权利的帐户 . 这将使您更容易学习基础知识(然后您可以在以后和/或 生产环境 中更安全地运行它) . Cloud Shell已安装和更新所有内容 .

    如果您必须通过本地计算机运行此快速入门,我建议您使用上面的第一个选项:"gcloud auth application-default login" . 您需要为操作系统安装Google Cloud SDK . 当您运行该命令时,它应该打开一个浏览器,系统将提示您登录您的Google Cloud帐户 . 这将授予您在本地运行脚本的权限 . 希望这可以帮助!

  • 3

    从本地计算机运行教程时,我也有相同的错误消息 . 我使用的是服务帐户(而不是“gcloud auth application-default login”),因为这是Google教程中推荐的首选方法 .

    但是,经过大量调查后,我发现问题是由于Google文档中的错误而发生的(似乎文档不是最新的) .

    Setting up authentication
    To run the client library, you must first set up authentication by creating a service 
    account and setting an environment variable. Complete the following steps to set up 
    authentication. For more information, see the GCP authentication documentation .
    
    GCP CONSOLECOMMAND LINE
    In the GCP Console, go to the Create service account key page.
    
    1. GO TO THE CREATE SERVICE ACCOUNT KEY PAGE
    2. From the Service account drop-down list, select New service account.
    3. In the Service account name field, enter a name .
    **4. From the Role drop-down list, select Project > Owner.**
    

    文档中的错误与说明的第4步有关 . 在GCP控制台的当前实现中,无法直接从“服务帐户密钥”页面设置角色 . 相反,您必须转到“IAM&admin”页面来设置“所有者”角色:

    In your Google Cloud console select “IAM & admin”->”IAM”
    
    You will see the “ADD” option. This will allow you to set permissions for your new 
    Service Account. Click “ADD”​. 
    
    You can then enter the service account and role ('Owner' if you are following the instructions in the tutorial).
    

    以下文章“为Google BigQuery设置Google Cloud Service帐户的缺失指南”提供了更多信息 . 本文是在BigQuery的上下文中编写的,但它同样适用于Google Datastore:

    https://blog.openbridge.com/the-missing-guide-to-setting-up-google-cloud-service-accounts-for-google-bigquery-6301e509b232

相关问题