【发布时间】:2019-12-29 05:22:31
【问题描述】:
基于 cron 样式部署设置 pub/sub 以调用 google 函数,该函数将检查新数据,然后将其推送到管道。此管道的一部分需要提交带有授权标头的 curl 调用,该授权标头采用身份令牌。我还没有找到生成此身份令牌的好方法。
我目前尝试将云功能的所有者更改为具有跨存储/数据标签/云功能权限的服务帐户,并且我还使用了带有私钥的存储凭证文件(即access.json)。我有一个指向此私钥的环境变量集 (GOOGLE_APPLICATION_CREDENTIALS),并尝试通过 $(gcloud auth application-default print-access-token) 在谷歌云函数中提取身份令牌 - 这将返回一个没有错误的空字符串。
# 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_Token> 是ID_Token 的空字符串,上述操作失败。
【问题讨论】:
标签: python-3.x google-cloud-platform google-cloud-functions google-oauth