i am trying to generate acces token but while doing so getting error that user code is expired.

Satyam Shukla 0 Reputation points
2023-05-31T09:00:12.7633333+00:00

i am trying to generate access token-

but when i am putting my user code and email and password after that getting error as follows-

The code you entered has expired. Get a new code from the device you're trying to sign in to and try again.

help me with the issue

app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)

flow = app.initiate_device_flow(scopes=SCOPES)
print(flow)
if 'user_code' not in flow:
    raise Exception('Failed to create device flow')

print(flow['message'])

result = app.acquire_token_by_device_flow(flow)

if 'access_token' in result:
    result = requests.get(f'{ENDPOINT}/me', headers={'Authorization': 'Bearer ' + result['access_token']})
    result.raise_for_status()
    print(result.json())

else:
    raise Exception('no access token in result')
Microsoft Security | Microsoft Authenticator
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. VasimTamboli 5,215 Reputation points
    2023-05-31T09:15:49.0733333+00:00

    you need to generate a new user code and try again.

    Here are the steps you can follow to generate a new access token:

    Make sure you have the latest version of the Microsoft Authentication Library (MSAL) installed.

    1. Update your code to initiate a new device flow and retrieve the updated user code:
    app = msal.PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
    flow = app.initiate_device_flow(scopes=SCOPES)
    
    if 'user_code' not in flow:
       
    
    
    2 people found this answer helpful.
    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.