javascript - 如何使用刷新 token 更新 outlook 2.0 API token ?

标签 javascript outlook outlook-restapi

我有下一个情况: 主要身份验证流程发生在服务器上,然后客户端获取这些数据,从那一刻起我希望客户端能够自行更新 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) {
    ...
  }
});

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

更新: 作为Alina Li在评论中指出她的回答,官方文档中有一个解决方案 https://learn.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参数。

引用自:

Storing Refresh Token In JavaScript, Getting New Access Token

关于javascript - 如何使用刷新 token 更新 outlook 2.0 API token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53154622/

相关文章:

javascript - 使用 Microsoft Graph 客户端创建日历事件

microsoft-graph-api - 使用 Microsoft Graph 从 session ID 获取第一封电子邮件

javascript - React 中 React Native 的 FlatList 等价物

javascript - 使用 setTimeout() 在 React 中渲染一个带有超时的列表

excel - 无法打开 .msg 文件

html - 将 html 文本设置为 VCALENDAR 的说明

javascript - 你能阻止 <img> 标签使用 CSS 加载它的图像吗?

javascript - IP 名称和路径显示在窗口上

c# - BackgroundWorker Completed 似乎没有在主线程中运行?

microsoft-graph-api - 微软图形 API : Is there any limit on payload size of a POST request