Bài này sẽ hướng dẫn chúng ta cách lấy access token và refesh token của azure bằng command
1) Create an Application on Azure.
Đầu tiên bạn cần tạo application:
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-17.png?v=1709747280)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-18.png?v=1709747436)
và dưới đây là thành quả
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-19.png?v=1709747591)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-20.png?v=1709747641)
Tiếp đến bạn cần click vào “add certificate and secret” để tại client secret.
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-21.png?v=1709747961)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-22.png?v=1709748013)
Và bạn sẽ cần copy value
Để bước này hoạt động được chúng ta cần tạo redirect-uri: http://localhost:6969
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-23.png?v=1709748407)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-24.png?v=1709748489)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-25.png)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-26.png?v=1709748713)
Generate Authorization Code (One time).
Bạn cần follow command này;
curl --location --request POST 'https://login.microsoftonline.com/<tenant-id>/oauth2/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=client_credentials' \ --data-urlencode 'scope=<client-id>.default' \ --data-urlencode 'client_id=<client-id>' \ --data-urlencode 'client_secret=<client-secret>' \ --data-urlencode 'resource=<client-id>'
Kết quả trả về trông giống như thế này:
{
"token_type": "Bearer",
"expires_in": 86399,
"ext_expires_in": 86399,
"access_token": "abcdefgh123456............."
}
Bạn pase test được chuẩn bị bên dưới lên browser
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize?client_id=<client-id> &response_type=code &redirect_uri=<redirect-uri> &response_mode=query &scope=<client-id>%2f.default&state=12345&sso_reload=true
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-27.png?v=1709749489)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-28.png?v=1709749513)
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-29.png?v=1709749599)
Nó sẽ có dạng như sau:
![](https://nimtechnology.com/wp-content/uploads/2024/03/image-30.png?v=1709750049)
Sau khi bạn past là editor nào đó thì bạn sẽ cần copy code = “” nó sẽ trước chữ state.
Get an auth token and a refresh token
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=<client-id> &scope=<client-id>%2f.default openid profile offline_access &code=<authorization-code> &redirect_uri=<redirect-uri> &grant_type=authorization_code &client_secret=<client-secret>' 'https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token'
Code thì bạn lấy ở bước trên.
và output như này:
{ "token_type": "Bearer", "scope": "User.Read profile openid email", "expires_in": 4557, "access_token": "eyJ0eXAiOiJKV1QiLCJub25jZSI6IkJuUXdJd0ZFc...", "refresh_token": "0.ARoAv4j5cvGGr0GRqy180BHbR8lB8cvIWGtHpawGN..." }
Generate new Access Token From a refresh token
curl --location --request POST 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'client_id=11111111-1111-1111-1111-111111111111' \ --data-urlencode 'refresh_token=OAAABAAAAiL9Kn2Z27UubvWFPbm0gLWQJVzCTE9UkP3pSx1aXxUjq...' \ --data-urlencode 'grant_type=refresh_token' \ --data-urlencode 'client_secret=jXoM3iz...'
ở đây tại mình chưa cấp quyền “User.Read Mail.Read” nên mình sử dụng được line –data-urlencode ‘scope=User.Read Mail.Read’
và cái respone chúng ta nhận về sẽ như sau
HTTP/1.1 200 OK Content-type: application/json { "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik5HVEZ2ZEstZnl0aEV1Q...", "token_type": "Bearer", "expires_in": 3599, "scope": "Mail.Read User.Read", "refresh_token": "AwABAAAAvPM1KaPlrEqdFSBzjqfTGAMxZGUTdM0t4B4...", }