I'm trying to read pull requests using the Azure DevOps Python API. I want to run something like this from the command line. The code below opens up a browser, but I don't know if I'm going about this the correct way. I can do this via PATs, but can't I just use a token I get from InteractiveBrowserCredential
instead? I can't find a way to do this without at PAT. Variations of the code below all tend to fail with various permission errors that aren't terribly helpful. Any help would be appreciated.
from azure.devops.connection import Connection
from azure.identity import InteractiveBrowserCredential
credentials = InteractiveBrowserCredential()
conn = Connection(base_url='https://dev.azure.com/xxxxx/', creds=BasicAuthentication('', credentials.get_token('?????')))
client = conn.clients.get_git_client()
result = client.get_pull_requests("my-test-repo")
You can generate token with InteractiveBrowserCredential
in Python and call Azure DevOps API with it.
In my case, I used below sample python code to connect to Azure DevOps:
from azure.identity import InteractiveBrowserCredential
import requests
credentials = InteractiveBrowserCredential()
access_token = credentials.get_token('499b84ac-1321-427f-aa17-267ca6975798/.default').token
#print(access_token)
print()
headers = {
'Authorization': 'Bearer {}'.format(access_token),
'Content-Type': 'application/json',
}
# Make a request to Azure DevOps API
response = requests.get('https://dev.azure.com/orgname/_apis/git/repositories/repoID/pullRequests?api-version=7.1-preview.1', headers=headers)
print(response.json())
When I ran above code, it asked me sign in with below screen to pick user account like this:
It displayed below page saying "Authentication complete" after successful authentication like this:
When I checked the console now, I got the response with pull requests details as below:
Reference: How to access azure devops apis via an azure app? - Stack Overflow by me