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