acquire_token_by_device_flow method not executing

Viewed 119
from msal import PublicClientApplication

TENANT_ID = '********-bca4-4be6-8a1d-********'
CLIENT_ID = '********-2f0b-4b9c-84ed-*******'

AUTHORITY = 'https://login.microsoftonline.com/consumers'
ENDPOINT = 'https://graph.microsoft.com/v1.0'

SCOPES = [
    'Files.ReadWrite',
    'Files.ReadWrite.All',
    'Sites.ReadWrite.All',
    'User.Read',
    'User.ReadBasic.All',
    'User.ReadWrite'
]


app = 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)
print(result)

print(flow) prints following data:

{'user_code': 'W57NE', 'device_code': 'SAQABAAEmX5eRRF9rmeBg4brEBmPRxtxI9LWyWTGPCHL6GEu7_PLvbhtOe9flSsOWYBPGifMtxiDU6gWx3n3qyNIc2MHmkghc74onQIzUzvQOJVXQgc5QH-wwjIAA', 'verification_uri': 'https://www.microsoft.com/link', 'expires_in': 900, 'interval': 5, 'message': 'To sign in, use a web browser to open the page https://www.microsoft.com/link and enter the code W57NE to authenticate.', 'expires_at': 1613721382.451286, '_correlation_id': 'ea1f9e06-39b1-47f5-bac0-e3d0188cabbd'}

I open the url in the browser and entered code mentioned in the response but it says The code that you entered has expired. Get a new code from the device you're trying to sign in to and try again.

Basically my use case is get access token using python script.

Thank you for help in advance.

0 Answers
Related