在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

huangapple go评论109阅读模式
英文:

Refresh token in Kiota MS Graph SDK for PHP

问题

我有Kiota SDK beta MS Graph for PHP,使用授权码进行身份验证。我不知道如何添加刷新令牌,因为现在每次重新加载页面都需要新的授权码。是否可以添加刷新令牌?

  1. $tokenRequestContext = new AuthorizationCodeContext(
  2. 'tenantId',
  3. 'clientId',
  4. 'clientSecret',
  5. 'authCode',
  6. 'redirectUri'
  7. );
  8. $scopes = ['User.Read', 'Mail.ReadWrite'];
  9. $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
  10. $user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();
英文:

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?

  1. $tokenRequestContext = new AuthorizationCodeContext(
  2. 'tenantId',
  3. 'clientId',
  4. 'clientSecret',
  5. 'authCode',
  6. 'redirectUri'
  7. );
  8. $scopes = ['User.Read', 'Mail.ReadWrite'];
  9. $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
  10. $user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();

答案1

得分: 1

请注意:要添加或生成刷新令牌,Azure AD应用程序必须被授予“offline_access”API权限。

我创建了一个Azure AD应用程序并授予了以下API权限:

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

现在,我使用以下端点生成了授权码:

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

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

通过使用以下参数,成功生成了访问令牌和刷新令牌:

  1. https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
  2. client_id:ClientID
  3. grant_type:authorization_code
  4. scope:user.read Mail.ReadWrite offline_access
  5. code:code
  6. redirect_uri:https://jwt.ms
  7. client_secret:ClientSecret

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

您可以使用以下方式刷新访问令牌:

  1. https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
  2. client_id:ClientID
  3. grant_type:refresh_token
  4. refresh_token:refresh_token
  5. client_secret:ClientSecret

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

通过以下方式修改代码以添加API权限:

  1. $tokenRequestContext = new AuthorizationCodeContext(
  2. 'tenantId',
  3. 'clientId',
  4. 'clientSecret',
  5. 'authCode',
  6. 'redirectUri'
  7. );
  8. $scopes = ['User.Read', 'Mail.ReadWrite', 'offline_access'];
  9. $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
  10. $user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();
英文:

> 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:

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

Now, I generated auth-code using below endpoint:

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

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

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

  1. https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
  2. client_id:ClientID
  3. grant_type:authorization_code
  4. scope:user.read Mail.ReadWrite offline_access
  5. code:code
  6. redirect_uri:https://jwt.ms
  7. client_secret:ClientSecret

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

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

  1. https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
  2. client_id:ClientID
  3. grant_type:refresh_token
  4. refresh_token:refresh_token
  5. client_secret:ClientSecret

在Kiota MS Graph SDK for PHP中刷新令牌 (Refresh token)

Modify the code by adding the API permission like below:

  1. $tokenRequestContext = new AuthorizationCodeContext(
  2. 'tenantId',
  3. 'clientId',
  4. 'clientSecret',
  5. 'authCode',
  6. 'redirectUri'
  7. );
  8. $scopes = ['User.Read', 'Mail.ReadWrite', 'offline_access];
  9. $graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
  10. $user = $graphServiceClient->users()->byUserId('[userPrincipalName]')->get()->wait();

huangapple
  • 本文由 发表于 2023年6月22日 05:07:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定