首页 > 解决方案 > Authentication, O Auth 2, Google Cloud Platform COS Container Optimized

问题描述

我在 Google Cloud 上使用容器优化的“COS”Linux 创建了一个 GCP 实例。该实例还从 Docker 存储库中提取,并在启动时托管多个容器。机器运转良好,实例处于活动状态。绝对没有错误。

当我尝试使用https://www.googleapis.com/../ 从外部访问它时,问题就出现了。.

我收到以下错误(在任何浏览器中):

    {
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Login Required.",
        "domain": "global",
        "reason": "required",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"
  }
}

我与 GCP 合作多年,从未见过此错误。不过我刚开始使用COS。我在想这可能与此有关?有什么提示吗?

标签: dockergoogle-cloud-platformoauth-2.0google-oauth

解决方案


在 GCE 中,存储 API 的默认访问范围是只读的,并且由于您的实例从 Docker 注册表中提取,因此您使用的服务帐户需要具有以下范围之一:

您可以在 Cloud Console 中的实例详细信息中或在 Cloud Shell 中使用以下命令检查和编辑实例的 API 范围:

  • 列出访问范围:curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/?recursive=true" -H "Metadata-Flavor: Google"
  • 添加范围:gcloud alpha compute instances [INSTANCE_NAME] --scopes=[SCOPE,...] --service-account=[SERVICE_ACCOUNT]. 将 [SCOPE] 替换为storage-rw(读/写)或storage-full(完全控制)。

推荐阅读