Unable to access to private repository from python

Hello :wave: , i have created an private repository and pushed my image to dockerhub. In my local client machine I had wrote a simple python script to check the information of that repository. however I always got the errors message
{ "code": "UNAUTHORIZED", "message": "authentication required",
my python code like this

import requests
import json
repo = "ryuuvu198/base_image"

USERNAME =  "......" => 
PASSWRD = "......." 

tag = "latest"
params = {
    'service': 'registry.docker.io',
    'scope': f"repository:{repo}:pull",
    'grant_type' : 'password',
    "username": USERNAME,
    "password": PASSWRD
}

url = 'https://auth.docker.io/token'
r = requests.get(url, params=params)

token = r.json()["token"]
print(json.dumps(r.json() , indent=2))

header = {"Accept": "application/vnd.docker.distribution.manifest.v2+json,application/vnd.docker.distribution.manifest.list.v2+json",
    "Authorization": f"Bearer {token}"}


r = requests.get(
    f'https://registry-1.docker.io/v2/{repo}/manifests/{tag}', headers=header)

if "errors" in r.json().keys():
    print("Error")
print(json.dumps(r.json() , indent=2))
digest = r.json()["config"]["digest"]
print("digest = " , digest)

how can I deal with this problem? please help me

Have you considered using the Docker SDK for Python?

https://docker-py.readthedocs.io/en/stable/client.html?highlight=login#docker.client.DockerClient.login

1 Like

HI, Thanks for your reply. i have resolved by using docker SDK for Python. docker login with user name password then I can access to the docker API no problem.