Python 3.x 谷歌云功能认证。获取身份令牌授权承载头
基于cron风格的部署设置发布/订阅,以调用google函数,该函数将检查新数据,然后将其推送到管道中。这个管道的一部分要求提交一个带有带有身份令牌的授权头的curl调用。我没有找到生成此身份令牌的好方法 我目前已尝试将云功能的所有者更改为具有跨存储/数据标记/云功能权限的服务帐户,并且我还使用了带有私钥的存储凭证文件(即Python 3.x 谷歌云功能认证。获取身份令牌授权承载头,python-3.x,google-cloud-platform,google-cloud-functions,google-oauth,Python 3.x,Google Cloud Platform,Google Cloud Functions,Google Oauth,基于cron风格的部署设置发布/订阅,以调用google函数,该函数将检查新数据,然后将其推送到管道中。这个管道的一部分要求提交一个带有带有身份令牌的授权头的curl调用。我没有找到生成此身份令牌的好方法 我目前已尝试将云功能的所有者更改为具有跨存储/数据标记/云功能权限的服务帐户,并且我还使用了带有私钥的存储凭证文件(即access.json)。我有一个环境变量集(GOOGLE\u APPLICATION\u CREDENTIALS),它指向此私钥,并尝试通过$(gcloud auth APP
access.json
)。我有一个环境变量集(GOOGLE\u APPLICATION\u CREDENTIALS
),它指向此私钥,并尝试通过$(gcloud auth APPLICATION default print access token)
在GOOGLE cloud函数中提取身份令牌-这将返回一个空字符串,没有错误
# I have tried something very similar to this
command = "echo $(gcloud auth application-default print-access-token)"
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.wait()
out = p.communicate()
print("OUT_CODE: ", out)
我只想用一个正确获得的令牌提交这个curl命令
command = "GOOGLE_APPLICATION_CREDENTIALS=/user_code/dl_access.json bash -c 'gcloud auth activate-service-account --key-file=/user_code/dl_access.json; echo $(gcloud auth application-default print-access-token)'"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
p.wait()
out, err = p.communicate()
auth = out.decode().rstrip()
print("OUT_CODE: ", out, err)
command = "curl -X POST "
command += '-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" '
command += '-H "Content-Type: application/json" '
command += 'https://datalabeling.googleapis.com/v1beta1/projects/'
command += '{}/datasets/{}/image:label '.format(PROJECT_ID, dataset.name.split("/")[-1])
command += "-d '{"
command += '"basicConfig": {'
command += '"labelGroup": "{}", '.format("test_label_group")
command += '"instruction": "{}", '.format("projects/cv/instructions/5cd5da11_0sdfgsdfgsdfg2c0b8eb8")
command += '"annotatedDatasetDisplayName": "{}", '.format(dataset.display_name)
command += '"replica_count": 3 '
command += '}, '
command += '"feature": "BOUNDING_BOX", '
command += '"boundingPolyConfig": { '
command += '"annotationSpecSet": "{}", '.format(
"projects/cv/annotationSpecSets/_b3b1_005csdfgc6_0000_297e1a11439bdc")
command += '}, '
command += "}' "
print(command)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
p.wait()
out, err = p.communicate()
print("out:", out)
print("err:", err)
由于
Authorization:Bearer
是ID\u Token
的空字符串,上述操作失败。您的云功能已沙盒化,无法执行系统调用。请记住,您处于无服务器模式,您不知道底层服务器存在什么问题:它自己的问题是什么?是否安装了gcloud和curl?哪个版本
因此,您必须编写代码并进行Python调用。结帐。
如果您喜欢直接调用API,也可以从代码中获得灵感。不要在云函数中使用shell脚本、外部命令等 下面是在云函数中运行时如何获取OAuth 2.0标识令牌的示例。在实际代码中,您需要将“受众”值更改为您调用的服务所需的任何值。如果调用此函数,它将在浏览器中显示“成功”或“失败”。这段代码还演示了如何使用Python请求库发出HTTP请求。使用此库,而不是尝试执行程序CURL
import requests
import json
def entry(request):
id_token = requestIdentityToken('http://www.example.com')
if id_token is not None:
print('ID Token', id_token)
return f'SUCCESS'
else:
return f'FAILURE'
def requestIdentityToken(audience=None):
host = 'http://metadata.google.internal'
header = {'Metadata-Flavor': 'Google'}
if audience is None:
audience = 'http://example.com'
url = '{}/computeMetadata/v1/instance/service-accounts/default/identity?audience={}'.format(host, audience)
try:
r = requests.get(url=url, headers=header)
if r.status_code < 200 or r.status_code >= 300:
print('Error:', r.reason)
return None
return r.text
except Exception as e:
print(str(e))
return None
此命令将“打印”您将在该函数的Stackdriver日志中找到的标识令牌
其他信息:
gcloud functions deploy requestIdentityToken --runtime python37 --trigger-http --entry-point entry