Microsoft Graph API -未接收刷新令牌



我正在使用下面的指南来为我的应用程序设置Oauth2。

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

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

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不包括offline_access,因此它只返回带有Files的访问令牌。读写权限。如果您想获得刷新令牌,请将offline_access添加到作用域。

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

最新更新