【问题标题】:Microsoft Graph API - not receiving refresh tokenMicrosoft Graph API - 未收到刷新令牌
【发布时间】:2021-05-13 15:59:39
【问题描述】:

我正在使用以下指南为我的应用设置 Oauth2。

https://docs.microsoft.com/en-us/graph/auth-v2-user

这是 /authorize URL 获取请求,工作正常:

GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&response_mode=query&scope=offline_access%20user.read%20files.readwrite

然后,我从 redirectUri 获取代码并 POST 到这个 URL:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token

返回一个 access_token 和一个 refresh_token。

但是,每当我需要刷新令牌时,Graph API 只会返回一个新的 access_token。 我正在使用 axios 和 qs:

//get new tokens

const scope = "Files.ReadWrite";

const data = qs.stringify({
    client_id: clientId,
    client_secret: clientSecret,
    grant_type: "refresh_token",
    redirect_uri: redirectUri,
    scope: scope,
    refresh_token: oneDriveRefreshToken
  });

  const headers = {
    "Content-Type": "application/x-www-form-urlencoded",
  };

  const response = await axios.post(tokenEndpoint, data, headers);  

  const json = response.data;

  functions.logger.debug(json.access_token); //ok
  functions.logger.debug(json.refresh_token); //undefined

据我了解,授权代码流以及“offline_access”范围应该使您能够在调用 /token 端点时获得新的刷新令牌

【问题讨论】:

  • 尝试将scope改为:const scope = "Files.ReadWrite offline_access";

标签: azure-active-directory microsoft-graph-api


【解决方案1】:

我注意到您在代码中定义的scope 不包括offline_access,因此它只是返回一个具有Files.ReadWrite 权限的访问令牌。如需获取刷新令牌,请在范围内添加offline_access。

尝试将scope 更改为:const scope = "Files.ReadWrite offline_access";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多