skip to Main Content

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!

2

Answers


  1. 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"])
    
    Login or Signup to reply.
  2. I have reproduced in my environment and got expected results as below and followed Microsoft-Document:

    from msal import ConfidentialClientApplication
    
    cid = "53f3ed37d4aeac"
    csecret = "AFK8MY-weud6cqP7jsvAJIojaq-"
    tid = "72f981db47"
    auth = f"https://login.microsoftonline.com/{tid}"
    
    app = ConfidentialClientApplication(
        client_id=cid,
        client_credential=csecret,
        authority=auth
    )
    s = "https://graph.microsoft.com/.default"
    result = app.acquire_token_for_client(scopes=s)
    a = result.get("access_token")
    print(a)
    

    Output:

    enter image description here

    Here csecret is client secret

    cid is client id

    tid is tenant id

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search