是否可以使用 API 密钥访问 Google Cloud Scheduler API?

Is it possible to access the Google Cloud Scheduler API using an API key?

是否可以访问 Google Cloud Scheduler API using an API key

方法:projects.locations.jobs.create https://cloud.google.com/scheduler/docs/reference/rest/v1/projects.locations.jobs/create

我正在尝试使用 curl 创建作业:

curl -X POST \
  'https://cloudscheduler.googleapis.com/v1/projects/my-project/locations/nam5/jobs?key=[MyAwesomeAPIKey]' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "test-awesome-job",
    "description": "My first job",
    "schedule": "45 23 * * 6",
    "timeZone": "utc",
    "pubsubTarget": {
        "topicName": "projects/my-project/topics/topic-name",
        "attributes": {
            "name": "39ro"
        }
    }
}'

但它会导致 401 未经授权的响应:

"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.",
  "status": "UNAUTHENTICATED"
}

Cloud Scheduler API 使用服务帐户凭据,如 https://cloud.google.com/docs/authentication/production 中所述。正如 API 文档所述,有限数量的 GCP 服务支持 API 键并且不包括 Cloud Scheduler。

如果您是 运行 与 App Engine、Cloud Functions 或 Cloud 运行 上的 Cloud Scheduler API 交互的代码,则服务帐户是 built-in 并且您需要做的就是授予该服务帐户通过 IAM 与 Cloud Scheduler 交互的权限。

文档提供了一些关于 the Cloud Scheduler client libraries 设置的更简化的信息。

看到 API 浏览器建议作为可能的凭据时,我感到很困惑 Google OAuth 2.0 或 API 密钥以及来自 Google Cloud API 凭据 (https://console.cloud.google.com/apis/credentials) 的 "Help me choose" 工具,显然现在报告了正确的解决方案:

For your situation you can use Application Default Credentials, 
which provide a simple way to access Google APIs from App Engine or Compute Engine.

之前它显示 API 键作为一个可能的选项。

感谢@Grayside指点我!