Dmi*_*riy 3 node.js office365 azure-active-directory outlook-restapi
我正在尝试创建一个 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;
}
Run Code Online (Sandbox Code Playgroud)
这是结果:
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
Run Code Online (Sandbox Code Playgroud)
我已经尝试了 PEM 格式的不同证书,其中包含“credentials”的“ca”属性。错误是一样的。
那么首先我可以使用自己颁发的PKI证书吗?PKI 证书有哪些要求才能与 Azure AD 配合使用?我使用SHA1算法和2048位加密。这些够了吗?
我还查看了 simple-oauth2 库的源代码,发现“ca”是唯一可以用于 PKI 设置的选项。这是明确检查的。所有其他 PKI 相关的 Nodejs https 选项(证书、密钥、密码等)都将被忽略,并且永远不会到达实际的请求代码。
我是唯一遇到这个问题的人吗?
该错误是因为在身份验证请求中使用了密钥而不是证书。就您而言,您根本不想使用秘密。听起来这里真正的问题是simple-oauth2库是否会处理将身份验证请求转换为 Azure 期望的格式。该格式的详细信息如下:Office 365 Rest API - 守护进程周身份验证。
我查看了自述文件simple-oauth2,他们的客户端凭证流示例使用秘密而不是基于证书的断言。查看配置代码,我看不到在那里进行任何基于证书的身份验证的能力,因此该库可能不适用于这种情况(除非我错过了)。
更新:好消息是adal-node 库确实支持使用证书,而且相当容易使用。棘手的部分是解决证书问题。
我已经启动了一个示例 Node.js 脚本,您可以在这里找到它: https: //github.com/jasonjoh/node-service。到目前为止,它仅使用来自 Azure 的证书检索令牌。自述文件包含我整理证书所经历的所有步骤。
| 归档时间: |
|
| 查看次数: |
3291 次 |
| 最近记录: |