计算引擎的服务帐户的云视觉范围不足 api

compute engine's service account has insufficient scopes for cloud vision api

我需要在我的 python 解决方案中使用 Cloud Vision API,我已经依赖 API 键一段时间了,但现在我尝试为我的 Compute Engine 的默认服务帐户提供调用 Vision 所需的范围,但到目前为止运气不佳。

我通过 cloud console 在我的项目中启用了视觉 API,但我仍然收到 403 错误:

Request had insufficient authentication scopes.

我会在我的 gce 的编辑详细信息选项卡中为每个 API 单独设置访问权限,但找不到与其他 API 一起列出的 Vision。 我设法从 Vision API 正确接收正确响应的唯一方法是再次从我的 gce 的编辑详细信息选项卡中标记 "Allow full access to all Cloud APIs" 复选框,但这对我来说听起来不太安全。

希望有更好的方法来做到这一点,但我在 Vision 的身份验证文档中找不到任何内容,在此处也找不到关于堆栈溢出的任何问题(有些主题很接近,但 none建议的答案非常适合我的情况,或提供了可行的解决方案)。

预先感谢您的帮助。


编辑

我正在添加每个 API 我可以从云控制台在我的 gce 默认服务帐户中单独启用的列表:

BigQuery; Bigtable Admin; Bigtable Data; Cloud Datastore; Cloud Debugger; Cloud Pub/Sub; Cloud Source Repositories; Cloud SQL; Compute Engine; Service Control; Service Management; Stackdriver Logging API; Stackdriver Monitoring API; Stackdriver Trace; Storage; Task queue; User info

None 它们似乎对我的需要有用,尽管启用对它们的完全访问全部解决了我的问题这一事实让我很困惑。


编辑 #2

我会尝试更简洁地陈述我的问题: 如何将 https://www.googleapis.com/auth/cloud-vision 添加到我的 gce 实例的默认帐户?

我正在寻找一种通过以下任何方法来实现此目的的方法:GCP 控制台、gcloud 命令行,甚至通过 Python(目前我正在使用 googleapiclient.discovery.build,不知道有没有办法通过图书馆求视野apiscope)。

或者只要通过 IAM 限制角色就可以启用所有范围吗?如果是这样,我该怎么做?

我真的找不到文档,再次感谢你。

Google Cloud APIs (Vision, Natural Language, Translation, etc) 不需要任何特殊权限,你应该在你的项目中启用它们(去 API Library 选项卡)并创建一个 API 密钥或一个服务帐户来访问它们。

考虑到 Service Accounts are the recommended approach for authentication with Google Cloud Platform services,您将 API 密钥转移到服务帐户的决定是正确的,并且出于安全原因,Google 建议使用它们而不是 API 键。

话虽如此,我看到您使用的是旧版 Python API Client Libraries, which make use of the googleapiclient.discovery.build service that you mentioned. As of now, the newer idiomatic Client Libraries are the recommended approach, and they superseded the legacy API Client Libraries that you are using, so I would strongly encourage to move in that direction. They are easier to use, more understandable, better documented and are the recommended approach to access Cloud APIs programatically

以此为出发点,我将把这个答案分为两部分:

使用客户端库

如果您决定听从我的建议并迁移到新的客户端库,那么身份验证对您来说真的很容易,因为客户端库使用应用程序默认凭据 (ADC) 进行身份验证。 ADC 使用 Compute Engine 的默认服务帐户来提供身份验证,因此您完全不必担心,因为它会默认运行。

清楚该部分后,您可以继续创建示例代码(例如 documentation), and as soon as you test that everything is working as expected, you can move on to the complete Vision API Client Library reference page 中提供的示例代码,以获取有关库如何工作的信息。

使用(旧版)API 客户端库 如果尽管我这么说,你还是想坚持使用旧的 API 客户端库,你可能会对这个其他文档页面感兴趣,其中有一些关于 Authentication using the API Client Libraries. More specifically, there is a whole chapter devoted to explaining OAuth 2.0 authentication using Service Accounts.

的完整信息

使用如下所示的简单代码,您可以使用 the google.oauth2.service_account module 从您首选 SA 的 JSON 密钥文件加载凭据,指定所需的范围,然后使用它通过指定 credentials=credentials:

构建 Vision 客户端时
from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-vision']
SERVICE_ACCOUNT_FILE = '/path/to/SA_key.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

vision = googleapiclient.discovery.build('vision', 'v1', credentials=credentials)

编辑:

我忘了补充一点,为了让 Compute Engine 实例能够使用 Google APIs,它必须被授予 https://www.googleapis.com/auth/cloud-platform 范围(在事实上,这与选择 Allow full access to all Cloud APIs) 是一样的。这记录在 GCE Service Accounts best practices, but you are right that this would allow full access to all resources and services in the project.

或者,如果您担心允许 "access-all" 范围的影响,在 this other documentation page 中解释说您可以允许完全访问,然后通过 IAM 角色执行限制访问。

无论如何,如果您只想实例授予Vision范围,您可以通过运行下面的gcloud命令来实现:

gcloud compute instances set-service-account INSTANCE_NAME --zone=INSTANCE_ZONE --scopes=https://www.googleapis.com/auth/cloud-vision

可以从 this page.

获得 Cloud Vision API 范围 (https://www.googleapis.com/auth/cloud-vision),对于任何其他 Cloud API

此外,如本节中有关 SA permissions and access scopes 所述,SA 权限应符合实例范围;这意味着将适用最严格的许可,因此您也需要牢记这一点。