【发布时间】:2020-01-16 06:43:15
【问题描述】:
我的脚本使用了一系列gcloud 命令,当然 gcloud 已经过身份验证。我需要使用 curl 来访问 gcloud 不可用的 GCP REST API。我可以通过在 Cloud Console 中生成 JSON 凭据文件来完成此操作,但我不想将其作为单独的手动步骤来执行。
[编辑:答案是将 curl 命令中的 gcloud auth 字符串替换为 gcloud auth print-access-token。请参阅下面的答案。]
我现在就是这样做的,使用从控制台下载的 JSON 文件。
GOOGLE_APPLICATION_CREDENTIALS=credentials.json
curl -X POST -H "Authorization: Bearer \"$(gcloud auth application-default print-access-token)\"" \
-H "Content-Type: application/json; charset=utf-8" \
https://cloudresourcemanager.googleapis.com/v1/projects/<MY_PROJECT>:getAncestry
如果没有下载的 JSON,我会得到这个:
ERROR: (gcloud.auth.application-default.print-access-token)
The Application Default Credentials are not available. They are available if running in Google Compute Engine.
Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing
to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials
for more information.
{ "error": {
"code": 401,
"message": "Request had invalid authentication credentials.
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"
}
}
如何利用我的gcloud 身份验证来使用 curl?如果我可以自动生成它作为这个脚本的一部分,我不介意使用 JSON。例如,有没有办法调用gcloud 来生成这个JSON,然后我将其设置为GOOGLE_APPLICATION_CREDENTIALS?
【问题讨论】:
标签: authentication curl google-cloud-platform gcloud