Suraj Naik
Suraj Naik

Reputation: 199

How to generate OAuth 2.0 token using TOTP?

enter image description hereHave this code in python:

import pyotp

totp = pyotp.TOTP('secret')

otp = totp.now()

How can I use otp generated above as a request parameter in the below API call to retrieve the OAuth2 token?

https://login.microsoftonline.com/{tenant-ID}/oauth2/v2.0/token

Upvotes: 0

Views: 645

Answers (1)

Jahnavi
Jahnavi

Reputation: 8008

Pass "otp"- value into the body of POST request as a part of payload with others like client_id, secret etc. and request for the token, the token was generated.

The data in the code are the params I took it from azure.

Find the below code for your reference.

import sys
import pyotp
import json
import requests 

secret = 'base32secret'    
totp = pyotp.TOTP(secret)
otp = totp.now()

API_ENDPOINT = "https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token"

headers = {
"Content-type": "application/json",
}  
data = {
'grant_type': 'client_credentials',
'username': 'someusrname',
'client_id': 'client_id',
'client_secret':'client_secret',
'scope':'api://{applicationId}/.default',
'otp': otp,
}

res = requests.post(url = API_ENDPOINT, data = data)
print("Access token is:", res.text)

enter image description here

Note: Make sure Oauth 2.0 is enabled to get the scope. How to enable it, check it out here

Upvotes: 1

Related Questions