I’m trying to get pull requests via the Azure DevOps Python API. The code below works, but I’d rather not use the /.default
scope because I only need a read-only call. The issue is if I put any other scope in the creds.get_token
call I get an error on the creds.get_token
call:
InteractiveBrowserCredential.get_token failed: Authentication failed: AADSTS65002: Consent between first party application 'xxxxx' and first party resource 'xxxxx' must be configured via preauthorization - applications owned and operated by Microsoft must get approval from the API owner before requesting tokens for that API.
The question is, what are least permissive scopes that I can put into creds.get_token
to fetch the pull requests as below:
creds = InteractiveBrowserCredential()
# I'd rather not use .default here
auth = BasicTokenAuthentication({'access_token': creds.get_token('499b84ac-1321-427f-aa17-267ca6975798/.default').token})
conn = Connection(base_url='https://dev.azure.com/xxx/', creds=auth)
client = conn.clients.get_git_client()
result = client.get_pull_requests("xxxx")
print(result)
2
Answers
The scope to access Azure DevOps Services REST API must be
499b84ac-1321-427f-aa17-267ca6975798/.default
, i’m afraid other scope is not supported. Please check similar ticket here for your reference.Initially, I too got same error when I ran the code with other scope(single DevOps permission) in the
creds.get_token
:If you prefer least permissive scopes to read pull request details without using
/.default
, you need to register one application and add vso.code permission in it like this:As you are using interactive flow to generate access token, make sure to add redirect URI in
Mobile and Desktop applications
platform by enabling public client flows:Now, you can make use of below modified code that generates access token using interactive flow with only vso.code permission instead of
/.default
and calls DevOps API to retrieve pull request details:Response:
You can also decode above access token in jwt.ms website and check scp claim to find permissions it has as below: