Share via

How to get v2 token

Maria Dąbrowiecka 105 Reputation points
2025-05-28T13:59:15.2233333+00:00

Hi team,

I'm trying to get the token based on the 'az login' I did manually in cmd before running the script:

class TokenProvider:
    def __init__(self):
        self.credential = AzureCliCredential()
        self.cached_token = None
        self.token_expires_at = 0

    def fetch_token(self, scope: str) -> AccessToken:
        current_time = time.time()
        if self.cached_token is None or current_time >= self.token_expires_at:
            token = self.credential.get_token(scope)
            self.cached_token = token.token
            self.token_expires_at = current_time + token.expires_on - 60
        return AccessToken(self.cached_token, self.token_expires_at)


self.token_provider = TokenProvider()
token = self.token_provider.fetch_token("https://management.azure.com/.default")
print(token.token)

but it looks like the token is V1 token. Is that correct? How can I obtain V2 token?

This is my az cli version:

az --version
azure-cli                         2.69.0
Azure Role-based access control
Azure Role-based access control

An Azure service that provides fine-grained access management for Azure resources, enabling you to grant users only the rights they need to perform their jobs.


Answer accepted by question author
  1. Anonymous
    2025-06-17T12:00:34.6666667+00:00

    Hello Maria Dąbrowiecka, Sorry for the delayed response.

    I did az login and generated access token using python code for custom API without interaction again:

    
    import subprocess
    
    import json
    
    import time
    
    from azure.core.credentials import AccessToken
    
    class TokenProvider:
    
        def __init__(self):
    
            self.cached_token = None
    
            self.token_expires_at = 0
    
        def fetch_token(self, scope: str) -> AccessToken:
    
            current_time = time.time()
    
            if self.cached_token is None or current_time >= self.token_expires_at:
    
                cmd = [
    
                    "az.cmd", "account", "get-access-token",
    
                    "--scope", scope,
    
                    "--output", "json"
    
                ]
    
                result = subprocess.run(cmd, stdout=subprocess.PIPE, check=True)
    
                token_data = json.loads(result.stdout)
    
                self.cached_token = token_data["accessToken"]
    
                self.token_expires_at = current_time + 3600  
    
            return AccessToken(self.cached_token, self.token_expires_at)
    
    provider = TokenProvider()
    
    token = provider.fetch_token("api://xxx/.default")
    
    print(token.token)
    
    

    User's image

    And what I observed is when we generate access token for any API via CLI then the Azure CLI (az login) always retrieves v1.0 tokens and we cannot change manifest of CLI app as its global app. Hence setting "requestedAccessTokenVersion": 2 in our application doesnt effect the token version.

    When I decoded the access token I see the appid ID Azure CLI ID not the Backend Microsoft Entra application ID:

    User's image

    Hence I can say that it is not possible to obtain v2.0 token via Azure CLI and without user interaction again.

    To get v2.0 token re-login is required and the below code will generate v2.0 token for only custom APIs

    
    from azure.identity import InteractiveBrowserCredential
    
    credential = InteractiveBrowserCredential()  
    
    token = credential.get_token("api://xxx/.default")  
    
    print(token.token)
    
    

    Hope this helps!


    If this answers your query, do click Accept Answer and Yes for was this answer helpful, which may help members with similar questions.

    User's image

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2025-06-03T00:50:01.19+00:00

    Hi @Maria Dąbrowiecka

    Thank you for the information,

    Based on the info, we have found that you are not providing any endpoint while fetching the token for the user and providing "https://management.azure.com/.default" as scope. If you would like to retrieve v2.0 token, your resource must accept v2.0 token. Only then you can retrieve the token with v2.0 version. In your scenario, by default you will receive v1.0 token itself as Azure service management is a Microsoft managed application where you cannot set the accepted token version to 2 for the application. By this I would like to confirm you that if you would like to fetch v2.0 token your resource must accept v2.0 token which is not possible with the current scope you are using to fetch the token. Here is the other way around if you would like to give a try on the same: https://stackoverflow.com/questions/60737010/what-scope-for-azure-resource-management-with-the-device-authorization-grant-typ

    You can also provide the feedback on the same in our feedback forum using the following link: https://feedback.azure.com/d365community

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


Your answer

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