Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 460
Eric Rohlfs Avatar asked Dec 18 '22 05:12

Eric Rohlfs


1 Answers

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

like image 191
Daniel Mann Avatar answered Apr 29 '23 02:04

Daniel Mann