好的,伙计们,问题来了。因为我使用的是密码授权,所以我没有点击 /oauth2/authorize 端点——密码授权不需要它——你直接去 /oauth2/token...
对于 MFA,/oauth2/authorize 是强制性的。如果启用了 MFA,它会为您重定向和处理所有内容(非常简单)。您只需等待您的重定向 url,身份验证代码是一个查询参数,因此很容易推断。
浏览器重定向后,您获取授权码,然后将其提交到 /oauth2/token 服务器,无需用户名/密码(也不需要授权标头,这很好,因为您不必要求两次 - 一次对于 MFA,并且一次传递给 /token - 好打电话给微软)。
流动
testMFA = function () {
var url = "https://login.microsoftonline.com/[tenantID]/oauth2/authorize?client_id=[clientID]&response_type=code&response_mode=query";;
var target = "_blank";
var options = "location=yes";
inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
with (inAppBrowserRef) {
try {
addEventListener('loadstart', loadStartCallBack);
addEventListener('loadstop', loadStartCallBack);
addEventListener('loaderror', loadStartCallBack);
addEventListener('exit', loadStartCallBack);
}
catch (ex) {
alert(ex);
}
}
}
然后,在“loadStartCallBack”中:
else if (event.url.split('/')[2] == '[returnURLWithoutHttps://]') {
var fullstring = event.url.split('/')[3].split('?code=')[1]
var code = fullstring.split('&')[0];
var sess_state = fullstring.split('session_state=')[1];
localStorage.tokenCode = code;
sessionStorage.sess_state = sess_state;
inAppBrowserRef.close();
getToken();
}
然后,您将授权代码传递到 /oauth2/token 服务器,并接收回您的令牌(我将在密码授予中留下评论,以供将来从密码授予开始的读者使用):
var data =
'resource=[resourceURL]' +
//'&username=' + window.sessionStorage.loginUser +
//'&password=' + password +
'&client_id=' + clientId +
'&code=' + authCode +
'&grant_type=authorization_code' +
//'&grant_type=password';
'&response_type=token';
var dataFinal = encodeURI(data);
就是这样。希望有一天它可以帮助某人。