Search code examples
pythonazureazure-ad-b2cazure-ad-msal

How can I get an ACCESS token using MSAL in python?


I cannot seem to figure out how to acquire an access token using MSAL. I’ve spend time reading the source code and Microsoft documentation to no avail. I’d like to use the PublicClientApplication to acquire the token. Even when running proof of concepts with the QuickStarts using ConfidentialClientApplication I seem to only get an ID_Token not an access token. Ultimately I’m trying to build a desktop/mobile app and want to be able to use MSAL for id token claims and access tokens for access to APIs. Also worth mentioning I want to implement this using AAD B2C Thanks!


Solution

  • To get your access use the acquire_token_silent or acquire_token_interactive methods of the PublicClientApplication class.

    Below is an example, replace the needed variables with your own.

    import msal
    
    app = msal.PublicClientApplication(
        "your_client_id", 
        authority="https://yourtenant.b2clogin.com/yourtenant.onmicrosoft.com/B2C_1_signup_signin_policy",
        client_credential=None
    )
    
    scopes = ["https://yourtenant.onmicrosoft.com/api/read"]
    
    result = app.acquire_token_interactive(scopes=scopes)
    
    print(result["access_token"])