0

I need to authenticate to Azure AD to perform REST API calls. I'm using Azure Python SDK (https://github.com/Azure/azure-sdk-for-python) for that. I have another code that returns me the JWT (JSON Web Token) of the user. How can I connect with this JWT? I try to look here https://learn.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate?tabs=cmd but I didn't find something useful

1 Answer 1

0

To call the REST API secured by azure ad, just make an API call with python, pass the token you got to the request header.

As you don't mention the specific API you want to call, here is just an approximate sample, change the url to the API you want and change requests.get to the method you want, there are maybe other headers and bodies depend on the specific API, the access_token is the token you should pass.

import requests

url = 'https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Network/virtualnetworks?api-version=2015-06-15'
headers = {'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + access_token}

response = requests.get(url=url,headers = headers)
print(response.status_code)
print(response.text)

Note: You don't provide the code you got the token, I don't know if it is correct, please make sure the audience of the token is correct and has the permission to call the corresponded API, otherwise you will get an error.

Reference:

Making a request to a RESTful API using python

How to make Raw REST Call for Azure using Python

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer but it doesn't use Azure python sdk
@Yossi You have already gotten the token, why it needs sdk? So what actually you want do do? Please include more details.
For context (for future visitors) the benefit of the SDK is it provides an object-based approach to interacting with Azure resources, GREATLY simplifying scripts that want to interact with Azure. Not having to write your own API abstraction layer saves a great deal of time (models, error handling, available methods, and above all well documented). I have the same question as the original poster, and will provide an update if I find a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.