我想通过https://gcr.io http API 从 Google Cloud Docker Registry 中检索图像列表。
我找到了一种从命令行执行此操作的方法,如下所示:
curl -u _token:$(gcloud auth print-access-token) https://gcr.io/v2/{project}/{repo}/tags/list
但我想在 Python 中以编程方式完成它。这是我到目前为止所尝试的:
下面的工作,但我找不到不调用gcloud
shell cmd 来检索身份验证令牌的方法。
import requests
import subprocess
command = "gcloud auth print-access-token"
pswd= subprocess.check_output(command, shell=True).decode().strip()
repo = "repo"
project = "myproject"
user = "_token"
url = "https://gcr.io/v2/{project}/{repo}/tags/list".format(project=project, repo=repo)
r = requests.get(url, auth=(user, pswd))
print (r.status_code, r.headers['content-type'], r.encoding, r.text)
此外,我还尝试使用经过身份验证的 httplib2 执行请求:
import httplib2
from oauth2client.client import GoogleCredentials
http = httplib2.Http()
credentials = GoogleCredentials.get_application_default()
scopes = "https://www.googleapis.com/auth/cloud-platform"
credentials = credentials.create_scoped(scopes)
http = credentials.authorize(http)
print (http.request("https://gcr.io/v2/healthshield-dev/dl/tags/list"))
结果是b'{"errors":[{"code":"UNAUTHORIZED","message":"Not Authorized."}]}'
有人可以和我分享他在这方面的经验吗?