谷歌云:身份验证范围不足

Jdr*_*uwe 1 google-cloud-platform kubernetes google-vision

我在向部署在 Google Cloud Kubernetes 集群中的 Spring Boot 应用程序发送请求时遇到困难。我的应用程序接收一张照片并将其发送到 Google Vision API。我正在使用提供的客户端库(https://cloud.google.com/vision/docs/libraries#client-libraries-install-java),如此处所述https://cloud.google.com/vision/docs/auth

如果您使用客户端库调用 Vision API,请使用应用程序默认凭据 (ADC)。使用 ADC 的服务在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中查找凭据。除非您特别希望让 ADC 使用其他凭据(例如用户凭据),否则我们建议您设置此环境变量以指向您的服务帐户密钥文件。

在我的本地机器上一切正常,我有一个带有环境的 docker 容器。varialbe GOOGLE_APPLICATION_CREDENTIALS 指向我的服务帐户密钥文件。

我的集群中没有这个变量。这是我从 Kubernetes 集群中的应用程序得到的响应:

{
    "timestamp": "2018-05-10T14:07:27.652+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "io.grpc.StatusRuntimeException: PERMISSION_DENIED: Request had insufficient authentication scopes.",
    "path": "/image"
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?提前谢谢!

Jdr*_*uwe 5

我还必须在 GKE 设置上指定 GOOGLE_APPLICATION_CREDENTIALS 环境变量,这些是我完成的步骤,感谢如何在通过 Kubernetes 运行的 GKE 上设置 GOOGLE_APPLICATION_CREDENTIALS

1. 创建密钥(在我的例子中是在 Gitlab 上的部署步骤中):

kubectl create secret generic google-application-credentials --from-file=./application-credentials.json
Run Code Online (Sandbox Code Playgroud)

2. 设置音量:

...
volumes:
- name: google-application-credentials-volume
  secret:
    secretName: google-application-credentials
    items:
    - key: application-credentials.json # default name created by the create secret from-file command
      path: application-credentials.json
Run Code Online (Sandbox Code Playgroud)

3. 设置卷挂载:

spec:
  containers:
  - name: my-service
    volumeMounts:
    - name: google-application-credentials-volume
      mountPath: /etc/gcp
      readOnly: true
Run Code Online (Sandbox Code Playgroud)

4.设置环境变量:

spec:
  containers:
  - name: my-service
    env:
    - name: GOOGLE_APPLICATION_CREDENTIALS
      value: /etc/gcp/application-credentials.json
Run Code Online (Sandbox Code Playgroud)