【问题标题】:How to use the API using only the API key如何仅使用 API 密钥来使用 API
【发布时间】:2021-05-16 14:20:07
【问题描述】:

我正在尝试使用 curl 从我的家用计算机运行 Compute Engine API(不要使用 gconsole 或 sdks)。 我可以使用 OAuth 2.0 访问获得响应,但不能仅使用 api-key 获得响应。 我可以只使用 API 密钥来使用 API 吗?

  • 成功

请求

curl https://compute.googleapis.com/compute/v1/projects/{PROJECT_ID}/zones/{ZONE}/instances/{INSTANCE_ID} 'Authorization: Bearer [ACCESS_TOKEN]'
  • 失败

请求

curl https://compute.googleapis.com/compute/v1/projects/{PROJECT_ID}/zones/{ZONE}/instances/{INSTANCE_ID}?key={MY_API_KEY}

回复

{
  "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"
  }
}

【问题讨论】:

  • Google Compute Engine API 不支持 API 密钥进行授权。

标签: curl google-cloud-platform google-compute-engine


【解决方案1】:

您需要了解公共数据和私有数据之间的区别。公共数据是不归任何人所有且可供公众使用的数据。要访问公共数据,您可以使用 api 密钥。

然而,有些方法是私人用户数据,为了访问它们,您需要获得访问该数据的授权

如果您查看方法 projects/get 的文档,您会看到它声明

这正是您的错误消息告诉您的内容

请求缺少所需的身份验证凭据。

所以答案是你不能。您不能使用 API 密钥访问您必须获得授权的私人用户数据。

它告诉你这个方法是用户数据,需要授权才能访问它。

【讨论】:

  • 我需要使用 OAuth 2.0 来访问私有 api。谢谢+1
猜你喜欢
  • 1970-01-01
  • 2022-12-30
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2019-04-22
  • 2019-02-18
相关资源
最近更新 更多