如何使用刷新令牌更新 outlook 2.0 API 令牌?

How to update outlook 2.0 API token using refresh-token?

我有下一个情况: 主要身份验证流程发生在服务器上,然后客户端获取这些数据,从那一刻起我希望客户端能够自行更新令牌。客户端似乎有所有需要的数据(access_token,refresh_token),但我不知道如何组织对 https://login.microsoftonline.com/common/oauth2/v2.0/token 路由的请求。

首先我试图获得 json 响应:

$.ajax({
  url: `https://login.microsoftonline.com/common/oauth2/token?grant_type=refresh_token&refresh_token=refresh_token&scope=openid%20profile%20offline%20access%20user.read%20mail.read%20contacts.read%20calendars.read&client_id=client&client_secret=secret`,
  type: 'POST',
  cache: false,
  processData: false,
  contentType: false,
  dataType: 'json',
  headers: {
    'Host': 'https://login.microsoftonline.com',
    'Content-Type': 'application/json'
  },
  success: function(data) {
    ...
  },
  error: function(xhr) {
    ...
  }
});

后来我发现只有重定向才能获取这些数据,对吗?如果是的话,有人可以举一个例子来说明如何实现这个,看起来需要创建一个 iframe 并以某种方式处理授权。谢谢

更新: 作为 Alina Li pointed in comment to her answer, there is a solution right in the official doc https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow

根据我的测试,您可以通过以下代码使用refresh-token:

var data = "grant_type=refresh_token&refresh_token=refreshToken&client_id=" + appState.clientId;
        $http = $http || $injector.get('$http');
        $http.post(authUrl + '/token', data, {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {

        }).error(function (err, status) {

        });

您不需要添加 scope 参数。

参考资料来自: