【问题标题】:Google Cloud Natural Language API - AuthorizationGoogle Cloud 自然语言 API - 授权
【发布时间】:2021-03-18 13:49:30
【问题描述】:

我正在尝试将 Cloud Natural Language API 与此链接中给出的代码一起使用。 (第一或第二) link

我收到以下错误。

google.api_core.exceptions.PermissionDenied: 403 Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are nots
upported by the language.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service
_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.

我知道我们需要按照 google doc (link) 在代码中提供服务帐户密钥的 json 文件。但它并没有说明处理 API 密钥。也不建议在代码中提供 API Key。那么方法是什么呢?

我尝试使用带有 API 密钥的 Curl 命令,它工作正常。

curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" -s -X POST -H "Content-Type: applicat
ion/json" --data-binary @request.json

但是我不知道如何将 API 密钥(我拥有的)合并到我上面发布的第一个链接中的代码中。

【问题讨论】:

  • 你在使用一些 SKD 吗?您使用哪种语言?
  • @R.Esteves 我正在使用 Python。我从启用了 NLP API 的同一个项目中打开了一个 cloudshell 编辑器。
  • 您是否尝试将os.eviron['GOOGLE_APPLICATION_CREDENTIALS'] = "/path/to/json/file" 放在代码的开头?

标签: google-cloud-platform google-api google-cloud-automl


【解决方案1】:

您是否完成了Google 提供的所有步骤?您可能错过了下面列出的步骤之一。

  • 在 GCP 中创建一个项目。

  • 为该项目启用计费。

  • 在该项目中启用自然语言 API。

      gcloud services enable language.googleapis.com --project [PROJECT_ID]
    
  • 使用您自己的帐户或服务帐户密钥(具有所有者权限)。

      gcloud auth application-default login
    

      export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
    
  • 通过命令行设置项目ID。

      gcloud config set project [PROJECT_ID]
    

【讨论】:

  • 我没有所有者权限。 “所有者”与我共享了一个可以访问 Cloud NLP 的 API_KEY。所以我试图弄清楚我将如何在那个谷歌文档的代码中使用这个密钥(在第一个链接中)
  • 我将 curl 命令与 API_KEY 一起使用,它工作正常。 Curl 的问题是我无法将它作为 csv 提供。两者都无法在该 Json 中提供多个条目。
  • 如果您正确设置了密钥文件身份验证,则不需要 API 密钥。正如我在上面发布的步骤中所述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
相关资源
最近更新 更多