Using a personal access token for azure devops API repository manipulation

只谈情不闲聊 提交于 2019-12-11 14:30:15

问题


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?


回答1:


I just scrapbooked following code which worked for me:

import requests
import base64

repo_endpoint_url = "https://dev.azure.com/<organization>/<project>/_apis/git/repositories?api-version=5.1"

username = "" # This can be an arbitrary value or you can just let it empty
password = "<your-password-here>"
userpass = username + ":" + password
b64 = base64.b64encode(userpass.encode()).decode()
headers = {"Authorization" : "Basic %s" % b64} 

response = requests.get(repo_endpoint_url, headers=headers)
print(response.status_code) # Expect 200


来源:https://stackoverflow.com/questions/58592919/using-a-personal-access-token-for-azure-devops-api-repository-manipulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!