Outlook Office 365:无法检索刷新令牌,因为 "AADSTS70000" 为 'code' 参数提供的值无效

Outlook Office 365 : Refresh token failed to retrieve because "AADSTS70000" the provided value for the 'code' parameter is not valid

在下面的代码中,我可以从 email@company.com 电子邮件地址成功检索刷新令牌。但是,当我尝试使用电子邮件@outlook.com 登录时,它不会提供刷新令牌,而是 returns 此响应。

回复:

{
  "error": "invalid_grant",
  "error_description": "AADSTS70000: The provided value for the 'code' parameter is not valid. The code has expired.\r\nTrace ID: ...\r\nCorrelation ID: ...\r\nTimestamp: 2016-05-19 10:13:05Z",
  "error_codes": [
    70000
  ],
  "timestamp": "2016-05-19 10:13:05Z",
  "trace_id": "8cceb393-....",
  "correlation_id": "5227de8...."
}

代码:

 private async Task<string> GetRefreshRoken(string authCode, string onSuccessRedirectUri) {
        var client = new HttpClient();
        var parameters = new Dictionary<string, string>
       {
          {"client_id", _clientId},
          {"client_secret", _clientSecret},
          {"code",authCode }, // what retreived from //https://login.microsoftonline.com/common with authroization.
          {"redirect_uri",  onSuccessRedirectUri}, //http://localhost:27592/Home/Authorize
          {"grant_type","authorization_code" }
       };
        var content = new FormUrlEncodedContent(parameters);
        var response = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token", content);
        var tokensJsonString = await response.Content.ReadAsStringAsync();
        dynamic token = Newtonsoft.Json.JsonConvert.DeserializeObject(tokensJsonString);
        return token.refresh_token;
    }

所以我在 google 上搜索了错误编号并找到了 http://www.matvelloso.com/2015/01/30/troubleshooting-common-azure-active-directory-errors/ 错误描述的页面:

然后我将我的重定向url更改为“http://localhost:27592/Home/Authorize/". Since I am using this https://dev.outlook.com/restapi/tutorial/dotnet教程作为参考,现在我无法使用任何其他帐户登录。

是否有任何好的方法来检索 outlook 帐户的刷新令牌?

对于 windows live id 帐户,您将收到错误消息“为 'code' 参数提供的值无效。代码已过期。 " 两次使用授权码时。

刷新令牌的正确方法是使用刷新令牌 (v2.0 token reference > Refresh Token).

首先,确保您已声明范围 "offline_access"。

然后,您将在使用grant_type=code(第一次获取令牌)获取令牌时获得access_token。

接下来,您需要使用grant_type=refresh_token刷新您的访问令牌。