刷新 Microsoft Access oktne 时获得无效授权

问题描述 投票:0回答:2

我已经创建了一个网络应用程序,用户可以将他的帐户与他的 outlook 帐户连接起来,这将允许他使用我的网络应用程序控制他的日历。

我获得了用户同意并获得了访问令牌和刷新令牌,一切正常。 当我刷新令牌时,可以说 30 分钟后我得到了新的访问令牌。

但是过了一会儿,有一天我想我开始收到这个错误

invalid_grant
当我刷新令牌时,我必须再次登录到 outlook 以获得有效的访问令牌。

有人知道为什么会这样吗?这只会在第二天发生 顺便说一下,我的范围和 API 许可中有

offline_access

这是我要发送的请求

curl --location --request POST 'https://login.microsoftonline.com/common/oauth2/v2.0/token' \
--header 'Origin: http://localhost:4200' \
--header 'Authorization: Bearer ${Access_Token}' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: fpc=AhDVWlE6NztJuqGq2Z_Q19xuuUk8AQAAAL3-ytsOAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd' \
--data-urlencode 'client_id=f4c91f15-2a7c-4b57-836d-5b11dbd37981' \
--data-urlencode 'scope=user.read calendars.readwrite offline_access openid profile' \
--data-urlencode 'refresh_token=${Refresh_Token}' \
--data-urlencode 'grant_type=refresh_token'

这是错误

azure-active-directory microsoft-graph-api refresh-token
2个回答
0
投票

我尝试在我的环境中重现相同的内容并得到以下结果:

我注册了一个 Azure AD 应用程序并添加了相同的 API 权限,如下所示:

enter image description here

我使用授权代码和 PKCE 流程通过具有以下参数的 Postman 生成了tokens

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
grant_type:authorization_code
client_id:<appID>
scope: user.read calendars.readwrite offline_access openid profile
code:code
redirect_uri: http://localhost:4200
code_verifier: S256

回复:

enter image description here

我能够使用此刷新令牌成功获取访问令牌直到 24 小时,如下所示:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id:<appID>
scope:user.read calendars.readwrite offline_access openid profile
refresh_token:<paste_refresh_token_from_above>
grant_type:refresh_token

回复:

enter image description here

当我在 24 小时后尝试刷新令牌时,我得到相同的错误如下所示:

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id:<appID>
scope:user.read calendars.readwrite offline_access openid profile
refresh_token:<paste_refresh_token_from_above>
grant_type:refresh_token

回复:

enter image description here

如果您将 redirect URI 注册为

SPA
,通常会发生错误,如下所示:

enter image description here

如本MS文档中所述,

对于单页应用,刷新令牌的默认生命周期为 24 小时。发送到注册为 spa

 的重定向 URI 的刷新令牌将在 24 小时后过期。

因此,您需要使用交互式身份验证重新运行授权代码流,以每 24 小时获取一个新的刷新令牌。

或者,您可以通过将

redirect URI 的平台设置为 Web

 来注册新的 Azure AD 应用程序,并使用正常的 
授权码 流程生成令牌,其中刷新令牌保留 90 天.


0
投票
非常感谢您的回答@sridevi

但是在将链接更改为 Web 而不是 SPA 后,当我尝试刷新令牌时出现以下错误

{ "error": "invalid_request", "error_description": "AADSTS90023: Cross-origin token redemption is permitted only for the 'Single-Page Application' client-type.\r\nTrace ID: 1f078ff3-ba01-44d7-b43c-dea2be005503\r\nCorrelation ID: d9e387a3-a7e7-44fe-aef0-f067421ec960\r\nTimestamp: 2023-04-17 12:50:14Z", "error_codes": [ 90023 ], "timestamp": "2023-04-17 12:50:14Z", "trace_id": "1f078ff3-ba01-44d7-b43c-dea2be005503", "correlation_id": "d9e387a3-a7e7-44fe-aef0-f067421ec960" }
这是我的要求

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.