Extract team members from Azure DevOps via API

Question:

i’m trying to make a simple API call from python to azure DevOps: just take the members of a team from an Azure DevOps project using the simple and yet elegant http library called Requests https://pypi.org/project/requests/.

Following this documentation from Azure to get the correct API https://learn.microsoft.com/en-us/rest/api/azure/devops/core/teams/get-team-members-with-extended-properties?view=azure-devops-rest-6.0&tabs=HTTP

Just a simple call to get the JSON response.

However, it is not working by simple making this on jupyter notebook:

import requests

response = requests.get(https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams/{teamId}/members?api-version=6.0)

(of course, replacing the variables between { } with the right values)

The response code that is coming is 203 and not 200. Probably i’m missing something related to the authentication to the DevOps project. But on this microsoft documentation i cannot find something that explain how to overcome this. Anyone can help me? How can i make this request and get the response using python jupyter notebook?
Thanks!

Asked By: Dan Hu

||

Answers:

You need to add your Auth:

response = requests.get(https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams/{teamId}/members?api-version=6.0, 
auth=('', 'Put Your Personal Access Token Here'))

enter image description here

Answered By: Minxin Yu – MSFT

Yeah. The issue was the authentication.

import requests

import base64

pat = ‘tcd******************************tnq’
authorization = str(base64.b64encode(bytes(‘:’+pat, ‘ascii’)), ‘ascii’)

headers = {
‘Accept’: ‘application/json’,
‘Authorization’: ‘Basic ‘+authorization
}

response = >requests.get(url="https://dev.azure.com/jack0503/_apis/projects?api-version=5.1", headers=headers)
print(response.text)

I found this sample code that solved my problem. Basically i needed to create an personal access token on the Devops project i want to access and them copy this token and paste on the pat variable. Then you just need to substitute the URL variable on the response.get method to make the request call to Azure Devops API.
In my case, the URL was for the team members

Solved 🙂 this is so good everyone wins.

Answered By: Dan Hu