GCP - 无法对自己进行身份验证以调用 Google 云功能
GCP - Unable to authenticate myself to invoke Google Cloud function
我有一个名为 rad_format_text_v0
的云函数。我 (andy@onehot.io) 有权调用它,如下所示:
$ gcloud beta functions get-iam-policy rad_format_text_v0
bindings:
- members:
- allAuthenticatedUsers
- user:andy@onehot.io
role: roles/cloudfunctions.invoker
etag: BwWOSfjYxp0=
version: 1
我可以使用 gcloud functions call
...
调用它
$ gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* andy@onehot.io
$ gcloud functions call rad_format_text_v0 --data "$(< test.json)"
executionId: 2wm7nrgc0vjo
result: |
["REDACTED successful result"]
但是,当我尝试使用另一个 HTTP 客户端(如 curl
)时,即使我正在传递身份验证令牌,它也会失败...
$ curl -i -X POST "https://us-central1-onehot-autocoder.cloudfunctions.net/rad_format_text_v0" -H "Content-Type:application/json" -H "Authorization: bearer $(gcloud auth application-default print-access-token)" --data @test.json
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token" error_description="The access token could not be verified"
Date: Mon, 22 Jul 2019 19:46:59 GMT
Content-Type: text/html; charset=UTF-8
Server: Google Frontend
Content-Length: 312
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/rad_format_text_v0</code>.</h2>
<h2></h2>
</body></html>
我完全按照 documentation 中的说明进行操作。我不知道为什么我的令牌不起作用。
您正在使用 gcloud auth application-default print-access-token
获取令牌,您共享的文档指定您应该改用 gcloud auth print-identity-token
命令。
我测试了它,发现我无法为我的用户帐户使用 print-identity-token
。相反,我不得不 create a new service account and activate it。然后在 curl 命令中,我指定了服务帐户,如下例所示:
curl -i https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME] -H "Authorization: bearer $(gcloud auth print-identity-token [SERVICE_ACCOUNT] )"
显然 Google Cloud SDK 在版本 254 上的 gcloud auth print-identity-token
命令有问题,您也可以尝试使用以下命令降级它:
gcloud components update --version 249.0.0
我有一个名为 rad_format_text_v0
的云函数。我 (andy@onehot.io) 有权调用它,如下所示:
$ gcloud beta functions get-iam-policy rad_format_text_v0
bindings:
- members:
- allAuthenticatedUsers
- user:andy@onehot.io
role: roles/cloudfunctions.invoker
etag: BwWOSfjYxp0=
version: 1
我可以使用 gcloud functions call
...
$ gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* andy@onehot.io
$ gcloud functions call rad_format_text_v0 --data "$(< test.json)"
executionId: 2wm7nrgc0vjo
result: |
["REDACTED successful result"]
但是,当我尝试使用另一个 HTTP 客户端(如 curl
)时,即使我正在传递身份验证令牌,它也会失败...
$ curl -i -X POST "https://us-central1-onehot-autocoder.cloudfunctions.net/rad_format_text_v0" -H "Content-Type:application/json" -H "Authorization: bearer $(gcloud auth application-default print-access-token)" --data @test.json
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token" error_description="The access token could not be verified"
Date: Mon, 22 Jul 2019 19:46:59 GMT
Content-Type: text/html; charset=UTF-8
Server: Google Frontend
Content-Length: 312
Alt-Svc: quic=":443"; ma=2592000; v="46,43,39"
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/rad_format_text_v0</code>.</h2>
<h2></h2>
</body></html>
我完全按照 documentation 中的说明进行操作。我不知道为什么我的令牌不起作用。
您正在使用 gcloud auth application-default print-access-token
获取令牌,您共享的文档指定您应该改用 gcloud auth print-identity-token
命令。
我测试了它,发现我无法为我的用户帐户使用 print-identity-token
。相反,我不得不 create a new service account and activate it。然后在 curl 命令中,我指定了服务帐户,如下例所示:
curl -i https://[REGION]-[PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME] -H "Authorization: bearer $(gcloud auth print-identity-token [SERVICE_ACCOUNT] )"
显然 Google Cloud SDK 在版本 254 上的 gcloud auth print-identity-token
命令有问题,您也可以尝试使用以下命令降级它:
gcloud components update --version 249.0.0