如何解决使用本地应用程序默认身份令牌对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
谢谢
解决方法
如果要将curl命令转换为python请求,请遵循enter link。这是转换后的代码:
导入请求
标题= { 'Authorization':'Bearer $ {gcloud auth print-identity-token)', }
response = requests.get('http:// SERVICE_URL',headers = headers)
,今天不可能。我在article中谈到了它。您需要一个服务帐户,才能使用Google Auth库生成有效的JWT令牌。所有库和所有语言都是相同的问题。
上周,我按下了merge request in the Java auth library以解决此问题。我不知道为什么Google不自己实现它。
在本地环境中,如果要在本地和云中使用相同的代码,则必须生成服务帐户密钥文件并将其与ADC一起使用。可悲的是,这是一个安全问题...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。