I have an app that needs to connect to Graph API with my customer's credentials to get data.
In order to generate the credentials I did the following (python code
from msal import PublicClientApplication
AUTHORITY = 'https://login.microsoftonline.com/common'
MICROSOFT_LOGIN_ADDRESS = 'https://login.microsoftonline.com'
def generate_token(client_id, scopes):
app = PublicClientApplication(client_id, authority=AUTHORITY)
flow = app.initiate_device_flow(scopes=scopes)
print(flow['message'])
token = app.acquire_token_by_device_flow(flow)
return token
token = generate_token(client_id, scopes)
refresh_token = token['refresh_token']
request_url =f"{MICROSOFT_LOGIN_ADDRESS}/{token['id_token_claims']['tid']}/oauth2/token
then I get use refresh_token
and request_url
to get access_token
:
payload = { "client_id": client_id,
"scope": scopes,
"client_secret": client_secret,
"grant_type": "refresh_token",
"refresh_token": refresh_token,
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url=request_url,
headers=headers,
data=payload)
access_token = json.loads(response.text)['access_token']
That worked fine until customer admin changed his password and I started to get an error:
"AADSTS50173: The provided grant has expired due to it being revoked, a fresh auth token is needed. The user might have changed or reset their password.״
I wonder what is the correct way to get the refresh token