I want to retrieve a list of images from Google Cloud Docker Registry via https://gcr.io http API.
I found a way to do it from command line like this:
curl -u _token:$(gcloud auth print-access-token) https://gcr.io/v2/{project}/{repo}/tags/list
But I want to do it in programmatically in Python. Here is what I tried so far:
The below works, but I can't find a way to retrieve authentication token without calling 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)
In addition, I have also tried to perform the request using authenticated 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"))
The result is b'{"errors":[{"code":"UNAUTHORIZED","message":"Not Authorized."}]}'
Can anybody share with me his experience with this?