Automating ARM Access Token Generation for Microsoft Video Indexer API using Python

Kasyfil Albar 20 Reputation points
2024-03-06T04:23:57.03+00:00

Currently, I'm using azure video indexer with ARM based token. I'm utilizing a bearer token obtained from https://learn.microsoft.com/en-us/rest/api/videoindexer/generate/access-token?view=rest-videoindexer-2024-01-01&tabs=HTTP&tryIt=true&source=docs#code-try-0 and then hitting the API to obtain an access token. Alternatively, I could open Azure to generate the access token manually. However, I need to automate this access token generation process programmatically using Python. Is there a way to achieve this?

Azure AI Video Indexer
Azure AI Video Indexer
An Azure video analytics service that uses AI to extract actionable insights from stored videos.
47 questions
{count} votes

Accepted answer
  1. navba-MSFT 17,365 Reputation points Microsoft Employee
    2024-03-06T05:01:27.64+00:00

    @Kasyfil Albar Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I was able to run the below python sample and generate the Access Token by invoking the VideoIndexer generate Access Token REST API.

    Before you run this sample, please update the variables accordingly:

    import requests
    
    # Define your Azure Video Indexer account details
    subscription_id = "b83c1ed3-XXX-XXX-XXX-2b83a074c23f"
    resource_group_name = "MY-Resources-Group"
    account_name = "MyIndexerName"
    
    # Define your Azure credentials
    client_id = "ad8a06b6-XXX-XXX-XXX-28fd86b57777"
    client_secret = "5dY8Q~pPmm7qjomXXXXXVqdXXXXXXXXXeakhS5aJo"
    tenant_id = "72f988bfXXX-XXX-XXX-2d7cd011db47"
    
    # Define the Azure endpoints
    auth_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/token"
    token_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.VideoIndexer/accounts/{account_name}/generateAccessToken?api-version=2024-01-01"
    
    # Get the AAD token
    aad_token_response = requests.post(
        auth_url,
        data={
            "grant_type": "client_credentials",
            "client_id": client_id,
            "client_secret": client_secret,
            "resource": "https://management.azure.com/"
        }
    )
    aad_token = aad_token_response.json()["access_token"]
    
    # Get the Video Indexer token
    vi_token_response = requests.post(
        token_url,
        headers={
            "Authorization": f"Bearer {aad_token}"
        },
        json={
            "permissionType": "Contributor",
            "scope": "Account"
        }
    )
    vi_token = vi_token_response.json()["accessToken"]
    
    print(vi_token)
    
    

    Here is the screenshot of the access token, I got from the above sample:

    User's image

    Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.

    **

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful