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
In my experience with doing this via other similar mechanisms, you have to include a leading colon on the PAT, before base64 encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With