'Using a personal access token for azure devops API repository manipulation

I have generated a personal access token from the Azure Devops user interface but am unable to use this to make requests against the Devops API.

I have tried many different header fields, but I am always redirected to the log in page as though I hadn't authenticated.

token = #Token generated on Devops project page
token_bytes = token.encode('utf-8')
token64 = base64.b64encode(token_bytes)
authorization_string = "basic " + str(token64)

repo_endpoint_url = "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1".format(organization=organization, project=project)

headers = {"Content-Type" : "application/json", "Authorization" : authorization_string}

response = requests.get(repo_endpoint_url, headers)

response is always 203 with login page HTML. This is what I'd expect to see if I didn't have an access token in the header.

I have tried, "Bearer" instead of "basic", I have tried adding {username}:{token}, and many other little tweaks.

What am I doing wrong?



Solution 1:[1]

After almost 1 week of investigating recreation of TOKEN was the only thing that helped me!

Solution 2:[2]

I had the same issue. This seems to me that if you create a PAT without a Code scope, and you want to add a Code scope you must regenerate PAT.

Same vice versa: If you want to add a Wiki scope to PAT with only a Code scope (for example you want to extend PAT which you have created in the REPOS section during the repository clone process) you must regenerate the PAT.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user15206036
Solution 2 Peter Trcka