Google 云 运行:从 GCP 外部调用
Google Cloud Run: Calling from outside GCP
我正在尝试访问安全云 运行 服务。如果我在我的机器上尝试使用 SDK,它工作正常:
curl --header "Authorization: Bearer $(gcloud auth print-identity-token)"
但是,我无法使用相同的服务帐户使用 Python API 来让它工作,我尝试使用 google-auth 来获取访问令牌但是这给了我一个 401 身份验证错误。这是我用来尝试获取令牌的代码:
import google.auth
import google.auth.transport.requests
scopes = ['https://www.googleapis.com/auth/cloud-platform']
creds, projects = google.auth.default(scopes=scopes)
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
# then using creds.token
查看文档:https://cloud.google.com/run/docs/authenticating/service-to-service#calling_from_outside_gcp it says to follow the sample code here: https://cloud.google.com/iap/docs/authentication-howto#iap_make_request-python我似乎无法按照指南中所说的启用 IAP,但似乎 IAP 仅适用于应用引擎而不适用于云 运行?
有人对此有什么建议吗?
谢谢
好的,似乎有一个 IDTokenCredentials class 适用于此,因为它使用 Open ID Connect ID 令牌而不是 OAuth 2.0 访问令牌:
https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
service_url = 'example.com'
key_file = 'key.json'
credentials = service_account.IDTokenCredentials.from_service_account_file(
key_file, target_audience=service_url)
authed_session = AuthorizedSession(credentials)
response = authed_session.get(service_url)
这很令人困惑,因为我没有在文档中看到它,它让我想到了一些关于 IAP 的东西,我认为它不适用于 Cloud 运行。
如果您仍想获取您的 ID 令牌 并且不使用 Dan 回复中的方法,则有更新的代码:
import requests
import google.auth
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
service_url = 'example.com'
key_file = 'key.json'
credentials = service_account.IDTokenCredentials.from_service_account_file(
key_file, target_audience=service_url)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
token = credentials.token
print('ID Token:', token)
我正在尝试访问安全云 运行 服务。如果我在我的机器上尝试使用 SDK,它工作正常:
curl --header "Authorization: Bearer $(gcloud auth print-identity-token)"
但是,我无法使用相同的服务帐户使用 Python API 来让它工作,我尝试使用 google-auth 来获取访问令牌但是这给了我一个 401 身份验证错误。这是我用来尝试获取令牌的代码:
import google.auth
import google.auth.transport.requests
scopes = ['https://www.googleapis.com/auth/cloud-platform']
creds, projects = google.auth.default(scopes=scopes)
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
# then using creds.token
查看文档:https://cloud.google.com/run/docs/authenticating/service-to-service#calling_from_outside_gcp it says to follow the sample code here: https://cloud.google.com/iap/docs/authentication-howto#iap_make_request-python我似乎无法按照指南中所说的启用 IAP,但似乎 IAP 仅适用于应用引擎而不适用于云 运行?
有人对此有什么建议吗?
谢谢
好的,似乎有一个 IDTokenCredentials class 适用于此,因为它使用 Open ID Connect ID 令牌而不是 OAuth 2.0 访问令牌:
https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
service_url = 'example.com'
key_file = 'key.json'
credentials = service_account.IDTokenCredentials.from_service_account_file(
key_file, target_audience=service_url)
authed_session = AuthorizedSession(credentials)
response = authed_session.get(service_url)
这很令人困惑,因为我没有在文档中看到它,它让我想到了一些关于 IAP 的东西,我认为它不适用于 Cloud 运行。
如果您仍想获取您的 ID 令牌 并且不使用 Dan 回复中的方法,则有更新的代码:
import requests
import google.auth
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
service_url = 'example.com'
key_file = 'key.json'
credentials = service_account.IDTokenCredentials.from_service_account_file(
key_file, target_audience=service_url)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
token = credentials.token
print('ID Token:', token)