使用本地应用程序默认身份令牌对Google Cloud python进行身份验证的云运行请求

问题描述

我正在尝试将following命令从CLI(有效)转换为python,但是遇到一些问题。

curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" SERVICE_URL

问题是我无法请求带有application default local credentials令牌的有效载体来向Google Cloud Run发出授权请求。如果我从CLI gcloud auth print-identity-token生成Bearer令牌并在python请求中使用它,则一切正常

request_url = 'https://<my endpoint>'
identity_token = '<>' # response of gcloud auth print-identity-token)
header= {'Authorization': f'Bearer {identity_token}'}
requests.get(url=request_url,headers=receiving_service_headers)

从google auth documentation起,我了解到Cloud Run通信基于支持模拟身份验证的身份令牌,但是我无法生成有效的凭据。

from google.auth import impersonated_credentials,default
from google.auth.transport.requests import AuthorizedSession

request_url = 'https://<my endpoint>'
source_credentials,project = default()

creds = impersonated_credentials.IDTokenCredentials(
    source_credentials,target_audience=request_url)

authed_session = AuthorizedSession(creds)
resp = authed_session.get(request_url)
print(resp)

我遇到以下错误

google.auth.exceptions.GoogleAuthError: Provided Credential must be impersonated_credentials

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)