Refresh token in Kiota MS Graph SDK for PHP

237 Views Asked by At

I have Kiota SDK beta MS Grapch for PHP with auth by code. I don't know how add refresh token because now after every reload page application require new auth code. Is posible add refresh token?

$tokenRequestContext = new AuthorizationCodeContext(
    'tenantId',
    'clientId',
    'clientSecret',
    'authCode',
    'redirectUri'
);

$scopes = ['User.Read', 'Mail.ReadWrite'];
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);

$user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();
1

There are 1 best solutions below

7
Rukmini On

Note that: To add or generate refresh token, the Azure AD Application must have offline_access API permission granted.

I created an Azure AD Application and granted API permissions like below:

enter image description here

Now, I generated auth-code using below endpoint:

https://login.microsoftonline.com/TenantID/oauth2/v2.0/authorize?
&client_id=ClientID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=user.read Mail.ReadWrite offline_access
&state=12345

enter image description here

Access token and refresh token got generated successfully by using below parameters via Postman.

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
grant_type:authorization_code
scope:user.read Mail.ReadWrite offline_access
code:code
redirect_uri:https://jwt.ms
client_secret:ClientSecret

enter image description here

You can refresh the access token by using refresh token like below:

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
grant_type:refresh_token
refresh_token:refresh_token
client_secret:ClientSecret

enter image description here

Modify the code by adding the API permission like below:

$tokenRequestContext = new AuthorizationCodeContext(
    'tenantId',
    'clientId',
    'clientSecret',
    'authCode',
    'redirectUri'
);

$scopes = ['User.Read', 'Mail.ReadWrite', 'offline_access];
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);

$user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();