Authenticating to VisualStudioOnline REST API with Personal Access Token using Python 3.6

ぃ、小莉子 提交于 2019-12-31 03:08:11

问题


I am trying to use the VisualStudioOnline REST API using python 3.6. (Plenty of examples using python 2.x.)

The python script response is the generic html login page.

I have tested the url generated by this script using REST Console Chrome plug-in and it worked fine using my personal access token.

import json
import base64
import urllib.request

personal_access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

headers = {}
headers['Content-type'] = "application/json"
headers['Authorization'] = b'Basic ' + 
base64.b64encode(personal_access_token.encode('utf-8'))

instance = "mycompany.visualstudio.com"
project = "MyProject"
repository ="MyRepository"
pullrequest = "3468"
api_version = "3.0"

repositories_url = ("https://%s/DefaultCollection/%s/_apis/git/repositories?
api-version=%s" % (instance, project, api_version))
print(repositories_url)

request = urllib.request.Request(repositories_url, headers=headers)
opener = urllib.request.build_opener()
response = opener.open(request)
print(response.read())

Powershell Example

How do I authenticate to Visual Studio Team Services with a Personal Access Token?

C# and curl example

https://www.visualstudio.com/en-us/docs/integrate/get-started/authentication/pats


回答1:


In my experience with doing this via other similar mechanisms, you have to include a leading colon on the PAT, before base64 encoding.



来源:https://stackoverflow.com/questions/45192108/authenticating-to-visualstudioonline-rest-api-with-personal-access-token-using-p

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