Attempting to set up a client credentials flow for B2C and receiving an "invalid_grant" error

Rian 0 Reputation points
2024-02-15T00:53:16.16+00:00

Wondering if there is any way to diagnose an "invalid_grant" error when attempting to set up a client credentials flow to allow programmatic access to an API. I have followed the directions as detailed here:
https://learn.microsoft.com/en-us/azure/active-directory-b2c/client-credentials-grant-flow?pivots=b2c-user-flow I have double-checked all the associated values. I'm attempting to acquire a token with Postman. The audit logs on Azure simply repeat the error. Any suggestions?

Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. Navya 20,180 Reputation points Microsoft External Staff Moderator
    2024-02-20T16:06:55.9+00:00

    Hi @Rian

    As you provide client-credentials flow parameters, I understand that you are passing valid token endpoint and parameter values. Could you confirm whether you have completed all the steps listed below to register applications?

    1.Register App2 and expose the scopes by setting the application id URI.

    254269-image.png

    1. Update manifest to define app roles.
        {  
            "allowedMemberTypes": [  
            "Application"  
            ],  
            "description": "B2CRole",  
            "displayName": "B2CRole",  
            "id": "1fb805ae-3118-4e7c-b5e0-032c289eaf44",  
            "isEnabled": true,  
            "lang": null,  
            "origin": "Application",  
            "value": "B2CRole"  
            },  
            {  
            "allowedMemberTypes": [  
            "Application"  
            ],  
            "description": "B2C",  
            "displayName": "B2C",  
            "id": "7316bf0a-f704-4bd4-9d9d-baf2d6f7719e",  
            "isEnabled": true,  
            "lang": null,  
            "origin": "Application",  
            "value": "B2C"  
            }],  
    
    
    

    3.Register the new one (app1) and update the app's accessTokenAcceptedVersion is set to 2.

    4.Create a client secret for app1

    5.Grant the app(app1) permission for API (app2).

    App1 -> API permission -> Add a permission -> select App2 with client id -> Select Application permission ->Select Add permissions.

    Then Grant admin consent for those permissions.
    254364-image.png

    5.Request the access token. The actual POST request looks like below:

    Replace tenant name and policy.

    Make sure to pass correct scope. e.g. https://<tenantName>.onmicrosoft.com/api/.default

    POST 
    
    https://<tenant-name>.b2clogin.com/<tenantname>.onmicrosoft.com/<policy>/oauth2/v2.0/token
    
    grant_type=client_credentials
    &client_id= Replace App1 client_id
    &client_secret= App1 client secret
    &scope= 
    

    Hope this helps. Do let us know if you any further queries.

    Thanks,

    Navya.

    0 comments No comments

Your answer

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