【问题标题】:Google Cloud Auto ML Vision Permission DeniedGoogle Cloud Auto ML Vision 权限被拒绝
【发布时间】:2020-08-05 12:35:36
【问题描述】:

我在 Google Cloud Platform Auto ML Vision API 中有一个奇怪的情况。当我进行 API 请求时,它总是抛出一个错误,说 PERMISSION DENIED,请参阅下面的示例错误响应:

{
  "error": {
    "code": 403,
    "message": "The caller does not have permission",
    "status": "PERMISSION_DENIED"
  }
}

这是我到目前为止所做的:

  1. 已创建服务帐户并下载包含服务帐户密钥的 json 文件。
  2. 为我的服务帐号启用了 AutoML Editor 和 AutoML Admin。
  3. 集成predict rest api并使用以下代码生成不记名令牌:

     var credentials: GoogleCredentials = GoogleCredentials
        .fromStream(context.assets.open("service_account_key.json"))
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"))
    credentials.refreshIfExpired()
    val accesstoken = credentials.accessToken
    

【问题讨论】:

    标签: android kotlin google-cloud-automl automl


    【解决方案1】:

    您能否将这些范围添加到您的凭据中:

     https://www.googleapis.com/auth/cloud-platform 
     #(View and manage your data across Google Cloud Platform services)
    
    
     https://www.googleapis.com/auth/cloud-vision
     #(Apply machine learning models)
    

    您也可以运行此命令来测试您的服务帐户是否具有 wright 权限。如果这可行,那么您的代码有问题。

    gcloud auth activate-service-account [ACCOUNT] --key-file=KEY_FILE
    curl -X GET -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" -H "Content-Type: application/json" https://automl.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/models
    

    【讨论】:

    • 运行你提到的命令后,它会抛出一个错误 "error": { "code": 403, "message": "您的应用程序已使用来自 Google Cloud SDK 或 Google Cloud 的最终用户凭据进行身份验证automl.googleapis.com 不支持的 Shell。我们建议在 gcloud 中配置 billing/quota_project 设置或通过 auth/impersonate_service_account 设置使用服务帐户。有关服务帐户以及如何在应用程序中使用它们的更多信息,见cloud.google.com/docs/authentication.", "status": "PERMISSION_DENIED" } }
    • 因为您必须使用您创建的服务帐户进行身份验证。错误消息说您使用用户凭据进行身份验证。请将 ACCOUNT 替换为您创建的服务帐户,并将 KEY_FILE 替换为此服务帐户的 key.json 文件。
    • 如何正确使用服务帐号进行身份验证?我已经有了 key.json 并直接从 assets 文件夹的代码中加载它。
    【解决方案2】:

    在“IAM & Admin”中创建一个新的服务帐户并下载一个新的 JSON 密钥文件(然后更新您的代码以反映新的文件名)。

    我只花了 12 个小时解决这个问题,并尝试了所有可能的解决方案。最后,唯一解决此问题的方法是创建一个新的服务帐户。

    我同时使用 Windows 和基于 Debian 的 Linux,这个问题不是特定于环境的。 我还在 Python 和 PHP 中使用我的 JSON 密钥。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2020-06-26
      • 2020-09-02
      • 2019-10-25
      • 2018-01-15
      • 2019-12-31
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多