I have a web API attempting to get set of access and refresh tokens for a B2C user. User sign-in is done through a configured "Social identity provider".
The API receives authorization code. When exchanging code for the tokens, Azure AD B2C tenant's endpoint endpoint returns invalid_grant error.
I have looked at the other answers I've found on the site. The issue still remains. Pointers are much appreciated.
Details as follows.
Sign-up and sign-in profiles issue claims for
- Given name
- Emails
- Identity provider
- Object ID
Error from token endpoint:
{
"error": "invalid_grant",
"error_description": "AADSTS70000: Transmission data parser failure: Authorization Code is malformed or invalid. [...]",
"error_codes": [
70000
],
[...]
}
Authorization request looks as follows:
AUTHORIZATION_ENDPOINT = 'https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/authorize'
authorization_url = f'{AUTHORIZATION_ENDPOINT}' \
f'?client_id={CLIENT_ID}' \
f'&response_type=code' \
f'&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fcode' \
f'&scope=openid offline_access' \
f'&nonce=hellobob' \
f'&p=B2C_1_<profile>'
Upon user authorization, authorization code is POSTed by API to token endpoint. Payload is represented as a Python dictionary.
TOKEN_ENDPOINT = 'https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token'
payload = {
'p': 'B2C_1_<profile>',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': 'http://localhost:8000/code',
'scope': 'openid offline_access'
}
response = requests.post(TOKEN_ENDPOINT, data=payload)