How to verify oauth token generated using Microsoft Graph API

S Andrew

I am getting the oath token using below code:

def get_token():

    try:
    
        r = requests.post("https://login.microsoftonline.com/" + config_data['TENNANT_ID'] + "/oauth2/token",
        
            data={"grant_type": "client_credentials",
                  "client_secret": config_data['CLIENT_SECRET'],
                  "client_id": config_data['CLIENT_ID'],
                  "resource": "https://graph.microsoft.com"})
                  
        if r.status_code == 200:
            ret_body = r.json()
            return ret_body['access_token']
            
        else:
            log.error("Unable to get token from oauth {}, {}".format(r.status_code, r.json()))
            return "false"
            
    except Exception as e:
        log.error("Exception occurred while getting oauth token {}".format(e))

I am looking for a microsoft graph api through which I can verify the generated oauth token weather its expired or not. Can anyone please point me to some documentation page for this.?

Hury Shen

As mentioned by Despicable in comments, when you access the token, the response json conntains a field expires_in. Below is the screenshot of response json when I request for access token, the value of expires_in is 82799 in my side but it may be 3599(1 hour) in your side.

enter image description here

You can use ret_body['expires_in'] in your code to get the field.

============================Update================================

As you can only receive the access token but not any more fields, so you can parse(decode) the access token to get the expire date.

When we parse the token in this page for test, we can find there is a claim exp(in timestamp format) which means the expire date of the token. So we just need to parse the token and get the property exp, then convert it from timestamp to datetime. enter image description here

Below is part of my code for your reference:

if r.status_code == 200:
    ret_body = r.json()
    accessToken = ret_body['access_token']
    decodedJson = jwt.decode(accessToken, verify=False)
    timestamp = decodedJson["exp"]
    resultDateTime = datetime.fromtimestamp(timestamp)

The resultDateTime is the expire time of your access token, you can compare it with current time(you can also skip change timestamp to datetime format in your code, compare the timestamp with current date timestamp directly).

To execute the code success, you also need to install pip install pyjwt and add these lines in your python code:

import jwt
import json
from datetime import datetime

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I access Azure Graph AND Microsoft Graph using same OAuth2 token?

How to set the access token lifetime for an app using the Microsoft Graph API

How to get Microsoft Graph API Access token using ajax call

Is there a way to verify an azure access token for Microsoft Graph API at the backend?

Not able to get access_token for Microsoft Graph API OAuth 2.0 using username & password

Microsoft graph API: Unable to fetch users with the generated access token

Unable to obtain Microsoft Graph OAuth access token

Using microsoft-ada-angular6 and microsoft-graph-client, how do I verify the permissions of the token I receive back from AAD?

Microsoft Graph API send email using Access Token of Enterprise Application

Get Access Token from Microsoft Graph API using PHP

Interact with OneDrive through Microsoft Graph API using Access Token

How to get AppKey using Microsoft Graph API

Microsoft Graph API - how to get access token without Authorization Code?

How do I create an auth token with the new microsoft graph api?

How to add the permissions in the access token of Microsoft Graph API

How to get Microsoft Graph API Access token from Node Script?

How to get Microsoft Graph API token to read mails

How can I verify login using google api, oauth?

How to refresh a token for Microsoft Graph

How to verify if published post using Facebook Graph API is set to public

how to find which application (client) id generated a specific access token - microsoft graph

Refreshing an OAuth access token for Microsoft Live API

How to refresh the token generated by lambda API using retrofit?

Microsoft Graph API: Omit user access token

Microsoft Graph API token validation failure

Access token for Microsoft Graph API is immediately expired

Microsoft graph api - no refresh_token

Microsoft Graph API not returning refresh token

Obtaining a valid access token for Microsoft Graph API