我正在尝试创建一个 NodeJS 守护程序/服务应用程序,以使用 node-outlook 库访问 Office 365 邮件/联系人。我能够创建 Office 365 试用订阅并注册我的应用程序。所以现在我可以访问我的应用程序的端点 URL、客户端 ID 和客户端密码。这是我的代码:
var outlook = require("node-outlook");
var token;
process.env.DEBUG = true;
var fs = require('fs');
var credentials = {
clientID: "<id>",
clientSecret: "<secret>",
site: "https://login.microsoftonline.com/<my-tenant-id>",
authorizationPath: "/oauth2/authorize",
tokenPath: "/oauth2/token",
useBasicAuthorizationHeader: false,
rejectUnauthorized: false,
ca: fs.readFileSync('pki/some.pem', { encoding: 'ascii' }),
};
var oauth2 = require('simple-oauth2')(credentials);
oauth2.client.getToken({}, saveToken);
function saveToken(error, result) {
if (error) {
console.log('Access Token Error: ', error);
return;
}
token = oauth2.accessToken.create(result);
var outlookClient = new outlook.Microsoft.OutlookServices.Client(
'https://outlook.office365.com/api/v1.0',
getAccessToken);
outlookClient.me.folders.getFolder('Inbox').messages.getMessages().fetchAll(10).then(
function (result) {
console.log('Success: ', result);
},
function (error) {
console.log('Error: ', error);
console.log('Headers: ', error.getAllResponseHeaders());
});
}
function getAccessToken() {
var deferred = new outlook.Microsoft.Utility.Deferred();
if (token.expired()) {
token.refresh(function (error, result) {
if (error) {
console.log("Refresh token error: ", error.message);
}
token = result;
deferred.resolve(token.token.access_token);
});
}
else {
deferred.resolve(token.token.access_token);
}
return deferred;
}
这是结果:
Error: { UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
readyState: 4,
onreadystatechange: [Function],
responseText: '',
responseXML: '',
status: 401,
statusText: null,
open: [Function],
setDisableHeaderCheck: [Function],
setRequestHeader: [Function],
getResponseHeader: [Function],
getAllResponseHeaders: [Function],
getRequestHeader: [Function],
send: [Function],
handleError: [Function],
abort: [Function],
addEventListener: [Function],
removeEventListener: [Function],
dispatchEvent: [Function] }
Headers: content-length: 0
server: Microsoft-IIS/8.0
request-id: 07931460-4fbf-4028-bc7b-fe350c240a1b
x-calculatedbetarget: BLUPR10MB0594.namprd10.prod.outlook.com
x-backendhttpstatus: 401
x-ms-diagnostics: 2000010;reason="The access token is acquired using an authenti
cation method that is too weak to allow access for this application. Presented a
uth strength was 1, required is 2.";error_category="insufficient_auth_strength"
x-diaginfo: BLUPR10MB0594
x-beserver: BLUPR10MB0594
x-powered-by: ASP.NET
x-feserver: CY1PR01CA0008
www-authenticate: Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trust
ed_issuers="00000001-0000-0000-c000-000000000000@*", token_types="app_asserted_u
ser_v1", authorization_uri="https://login.windows.net/common/oauth2/authorize",
error="invalid_token",Basic Realm="",Basic Realm=""
date: Mon, 29 Jun 2015 18:34:32 GMT
connection: close
我已经尝试了 PEM 格式的不同证书,其“ca”属性为“credentials”。错误是一样的。
那么首先,我可以使用自发的 PKI 证书吗?PKI 证书有哪些要求才能与 Azure AD 一起使用?我使用 SHA1 算法和 2048 位加密。这些够了吗?
我还查看了 simple-oauth2 库的源代码,发现“ca”是唯一可以用于 PKI 设置的选项。它被明确检查。所有其他与 PKI 相关的 nodejs https 选项(证书、密钥、密码,...)都被忽略,永远不会得到实际的请求代码。
我是唯一遇到问题的人吗?