How can genereate token for rest api?

Akash Sharma 0 Reputation points
2024-12-06T04:53:18.3933333+00:00

When we run the Azure REST API, it generates an Authorization Bearer token. I tried generating the token using the following method, but with this token, I can only access data for a single resource group. How can I get subscription-level access?

f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
    data = {
        "grant_type": "client_credentials",
        "client_id": client_id,
        "client_secret": client_secret,

Resource

AZURE_BASE_URL="https://management.azure.com"

How can I get this autherization token ?User's image

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,648 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Raja Pothuraju 10,040 Reputation points Microsoft Vendor
    2024-12-10T20:36:10.15+00:00

    Hello @Akash Sharma,

    Thank you for posting your query on Microsoft Q&A.

    Based on your description, I understand that you are generating a bearer token using the OAuth 2.0 client credential flow with parameters such as client_id, client_secret, and tenant_id for the scope "https://management.azure.com". Using the token, you were able to retrieve details for a single resource group, and now you want to do the same for all resource groups in your subscription. Please let me know if my understanding is incorrect.

    The process you followed to generate the bearer token and retrieve subscription-level data is correct. With the token, you can access and manage all resource groups created under that subscription. Below are additional details and examples to guide you further:

    Here is an example of the bearer token retrieval:

    enter image description here

    Using the above access token, you can retrieve details of all resource groups by making a request similar to the following:

    POST https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.CostManagement/query?api-version=2022-10-01  
    

    enter image description here

    If you encounter any error message while attempting to make a call to another resource group, please attach a screenshot for better understanding and troubleshooting of the issue.

    I hope this information is helpful. Please feel free to reach out if you have any further questions.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Thanks,
    Raja Pothuraju.

    0 comments No comments

  2. Akash Sharma 0 Reputation points
    2024-12-12T17:20:09.6766667+00:00

    Hi @Raja Pothuraju ,
    How can I generate this bearer token? The token is working fine for me. I have tried the one you provided, but I am still only able to access a single resource group's data.

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.