Showing posts with label active directory access token refresh token. Show all posts
Showing posts with label active directory access token refresh token. Show all posts

Saturday, September 17, 2022

Microsoft API - Get Access Token and Refresh Token

1. Log into Azure Portal Active Directory.

2. Register your application.

3. Create secret for your application.

4. Find your Client ID and Tenant ID.

5. Generate Authorization  Code. (One Time)

https://login.microsoftonline.com/

{Tenant ID}/oauth2/v2.0/authorize?client_id={AppReg ID} &response_type=code &redirect_uri=http%3a%2f%2flocalhost%3a8080 &response_mode=query &scope={AppReg ID}%2f.default&state=12345&sso_reload=true 

6. Save redirect url. It is Authorization Response.  

7. Generate Refresh Token. (One Time)

curl -X POST -H 

"Content-Type: application/x-www-form-urlencoded" 

-d 'client_id={AppReg ID}
  &scope={AppReg ID}%2f.default openid profile offline_access
  &code={authorization code}
  &redirect_uri=http%3A%2F%2Flocalhost%3a8080
  &grant_type=authorization_code
  &client_secret={AppReg Secret}' 

'https://login.microsoftonline.com/{Tenant ID}/oauth2/v2.0/token'

8. Generate Access Token from Refresh Token (Every Time)

curl --location --request

POST 'https://login.microsoftonline.com/

{Tenant ID}/oauth2/v2.0/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'client_id={AppReg ID}' \ --data-urlencode 'scope={scope returned in previous request}' \ --data-urlencode 'refresh_token={Refresh Token}' \ --data-urlencode 'grant_type=refresh_token' \ --data-urlencode 'client_secret={AppReg Secret}'