使用 Google API 的身份验证范围不足错误
Posted
技术标签:
【中文标题】使用 Google API 的身份验证范围不足错误【英文标题】:Insufficient authentication scopes error using Google API 【发布时间】:2017-03-10 23:29:54 【问题描述】:我将 Google SDK 设置为使用带有 Application Default Credentials 的 Google API。对于我的本地机器,创建了一个凭证 json 文件并将其路径设置为 GOOGLE_APPLICATION_CREDENTIALS
作为环境变量。这按预期工作,没有问题。
但是,当应用部署到 Google Cloud VM 时,会引发以下错误:
[Google_Service_Exception]
"error":
"code": 403,
"message": "Request had insufficient authentication scopes.",
"errors": [
"message": "Request had insufficient authentication scopes.",
"domain": "global",
"reason": "forbidden"
],
"status": "PERMISSION_DENIED"
根据documentation,内置服务帐户应该与虚拟机实例相关联。为了使它工作,我尝试像在本地机器上一样使用凭证 json 文件(效果很好),但它也没有工作。
请注意,错误消息是关于范围而不是身份验证问题。如何使其在 Compute Engine 虚拟机实例上运行?
给客户端初始化代码:
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Pubsub::PUBSUB);
【问题讨论】:
【参考方案1】:更新
现在支持了。您必须停止实例才能更改 API 范围。 https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes
原答案
事实证明,我必须为 Google Cloud Console 上的服务启用 Cloud API 访问范围以获取 VM 实例详细信息:https://console.cloud.google.com/compute/instances
很遗憾,我无法更改它,因为 Google Cloud 目前不支持它。我必须启动一个新实例来启用服务 API 访问。 https://googlecloudplatform.uservoice.com/forums/302595-compute-engine/suggestions/13101552-ability-to-change-cloud-api-access-scopes-on-launc
【讨论】:
从 2016 年 12 月起,您似乎可以更改已停止虚拟机的访问范围。这个链接现在有回复了:googlecloudplatform.uservoice.com/forums/302595-compute-engine/…【参考方案2】:现在可以了。您的实例必须停止,然后它可以在编辑 vm 页面中的控制台或 SDK 中使用以下命令更改其范围列表:
gcloud compute instances stop [vmname]
gcloud beta compute instances set-scopes [vmname] --scopes="[scopes list]"
请注意,使用 SDK 方式,第二个命令将使用列表中的范围进行重置。当前不提供仅附加新范围的功能。
【讨论】:
以上是关于使用 Google API 的身份验证范围不足错误的主要内容,如果未能解决你的问题,请参考以下文章
Google Calendar API - 请求的身份验证范围不足
Youtube 评论 API 抛出“权限不足:请求的身份验证范围不足”错误
(403) 请求的身份验证范围不足 - OAuth 范围 Google Clas-s-room API
如何使用 C# 向谷歌聊天室发布消息? (**错误**:请求的身份验证范围不足)