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.
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)
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:
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.
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.