【问题标题】:How to get the oauth refresh token?如何获取 oauth 刷新令牌?
【发布时间】:2019-05-02 01:52:57
【问题描述】:

我有一个使用 Oauth 的 Web 应用程序,但由于以下错误而无法重用 authToken。

{"AADSTS70002: Error validating credentials. AADSTS54005: OAuth2 Authorization 
code was already redeemed, please retry with a new valid code or use an 
existing refresh token.\r\nTrace ID: 30c342a7-f16a-4a05-a4a8- 
c7ee2c722300\r\nCorrelation ID: 3a5c99d1-ca1c-4cd7-bd36- 
cce721bf05b6\r\nTimestamp: 2018-11-21 00:26:18Z"}

有人告诉我这是一个已知问题/更新 herehere。 ...好的,很好,所以现在我正在尝试获取刷新令牌,以便我可以重新生成我的访问令牌,但我无法让某些工作正常工作。

我已经尝试了以下方法: https://docs.microsoft.com/en-us/bingads/shopping-content/code-example-authentication-oauth - 当我尝试获取访问令牌或刷新令牌时,这个似乎不起作用并引发异常。说明发生了一个或多个错误。

https://auth0.com/docs/api/authentication#authorization-code-pkce- - 但不返回刷新令牌。这可能是因为我没有 code_verifier 吗?如果是这样,我将如何获得它?

Authorization Code (PKCE) Image

下面是我正在使用的代码示例 - 这里的问题是我只能使用一次,一旦它已被修复,我无法静默检索它,因为它不再存在于缓存中。

ClientCredential clientcred = new ClientCredential(Constants.ClientId, Constants.AppKey);

 TokenCache TC = new TokenCache();

 AuthenticationContext AC = new AuthenticationContext(Constants.Authority, TC);
 //Set token from authentication result
 AuthenticationResult authenticationResult = await AC.AcquireTokenByAuthorizationCodeAsync(
            Constants.Code,
 new Uri(Constants.postLogoutRedirectUri + "Index"), clientcred);

 return authenticationResult.AccessToken;

【问题讨论】:

  • 你连接的是什么认证服务器?
  • 您找到解决方法了吗?我有同样的问题。

标签: c# oauth oauth-2.0 access-token


【解决方案1】:

您需要调用具有 offline_access 范围的 OAuth2 授权端点来获取刷新令牌。

您应该只在收到授权码时调用 AcquireTokenByAuthorizationCodeAsync 一次,并且不应使用结果。 azure ad sample

当您想要获取访问令牌时,您需要静默调用 AcquireToken。 azure ad sample

这个azure ad sample通过用户ID使用TokenCache实现。

授权请求

令牌请求

祝你好运!

【讨论】:

  • 您的令牌屏幕截图看起来就像我在描述中链接的屏幕截图。然而,我错过了回复 uri。这就是获取刷新令牌和不获取的区别吗?
  • offline_access进入授权请求。
猜你喜欢
  • 2021-01-18
  • 2012-04-18
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 2015-11-26
  • 2020-05-05
  • 2015-07-15
相关资源
最近更新 更多