Google Cloud Vision OCR 错误代码 7 - 权限被拒绝

Google Cloud Vision OCR Error Code 7 - Permission Denied

我正在构建一个利用 Google Cloud Vision 的 OCR 的网络应用程序。 OCR 可以正常处理大约 7-8 个请求,之后我收到如下错误:

Error: 7 PERMISSION_DENIED: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the vision.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/.

问题是,我已经设置了一个结算帐户和一个服务帐户。

我尝试使用多个 GCloud 命令来解决此问题,当我 运行 gcloud auth list 时,我可以看到我的服务帐户是活动帐户。我还尝试生成一个 JSON 密钥并在我的环境变量中设置该密钥的路径 - 按照此处的说明:https://cloud.google.com/docs/authentication/getting-started

有没有人遇到过这个问题?作为参考,我 运行ning Windows 10 并使用 Node.js 作为 webapp。谢谢!

您正在使用来自 Google Cloud SDK 或 Google Cloud Shell 的最终用户凭据而不是服务帐户凭据进行身份验证。

1.Make一个新目录

mkdir ocr
cd ocr

2.Download 一张图片。

curl https://www.python.org/static/apple-touch-icon-144x144-precomposed.png > image.png

3.Install 客户端库。

sudo pi3 install --upgrade google-cloud-vision

4.Create 一个服务帐户。

gcloud iam service-accounts create ocr-vision \
      --description "ocr-vision" \
      --display-name "ocr-vision"

gcloud iam service-accounts list

5.Create 一个 key.json 文件。

gcloud iam service-accounts keys create key.json \
      --iam-account ocr-vision@your-project.iam.gserviceaccount.com 

6.Assign 服务帐户的所有者角色。

gcloud projects add-iam-policy-binding your-project \
      --member serviceAccount:ocr-vision@your-project.iam.gserviceaccount.com \
      --role roles/owner

7.Export 环境变量

export GOOGLE_APPLICATION_CREDENTIALS=key.json

8.Run 脚本

 python script.py
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.abspath('image.png')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)

# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)

9.Output

Labels:
Yellow
Font
Line
Material property
Clip art
Logo
Symbol
Icon
Graphics
Illustration