刷新Microsoft Live API的OAuth访问令牌

时间:2015-02-05 03:53:20

标签: rest oauth access-token microsoft-live-connect

目前,我让用户通过在Web视图中向以下网址发送请求来登录Microsoft Live:

https://login.live.com/oauth20_authorize.srf?client_id=[CLIENT ID]&scope=[SCOPES]&response_type=token&redirect_uri=[REDIRECT URI]&display=popup

这完美无缺,我收到并保存了access_tokenauthentication_token。请注意,即使我包含refresh_token范围,它也不会返回wl.offline_access

当访问令牌过期并需要刷新时,会出现此问题。我正在尝试使用Microsoft's documentation中的方法刷新令牌:

https://login.live.com/oauth20_token.srf?client_id=[CLIENT ID]&redirect_uri=[REDIRECT URI]&client_secret=[CLIENT SECRET]&refresh_token=[WHAT TO PUT HERE?]&grant_type=refresh_token

但是,登录中从未返回refresh_token,因此我不确定要传入的内容。请注意发送authentication_token(它应该用于什么?)as refresh_token参数会产生以下结果:

{
  "error": "invalid_grant",
  "error_description": "The provided value for the input parameter 'refresh_token' is not valid."
}

有谁知道如何通过REST API正确刷新Microsoft Live令牌?

1 个答案:

答案 0 :(得分:8)

在通过微软documentation进一步阅读并进行实验后,我能够弄清楚如何做到这一点。

我最初尝试的问题是我在使用implicit grant flow时请求wl.offline_access范围,因为他们的文档说不是:

  

注意如果您正在使用,请不要包含wl.offline_access范围   隐式授权流程(response_type=token)。

因此,我将网址更改为以下内容(使用authorization code grant flow,因为我需要离线访问):

https://login.live.com/oauth20_authorize.srf?client_id=[CLIENT ID]&scope=[SCOPES]&response_type=code&redirect_uri=[REDIRECT URI]&display=popup

然后,一旦我在回调中收到code,我就调用了以下端点来检索访问权限并刷新令牌:

https://login.live.com/oauth20_token.srf?client_id=[CLIENT ID]&redirect_uri=[REDIRECT URI]&client_secret=[CLIENT SECRET]&code=[CODE FROM AUTHORIZATION]&grant_type=authorization_code

注意:上述链接中此端点的Microsoft文档 INCORRECT 。这是GET请求,而不是文档声明的POST请求。

此方法最后返回了access_tokenrefresh_token参数,我可以按预期使用这两个参数。