我正嘗試使用node-outlook庫創建NodeJS守護程序/服務應用程序以訪問Office 365郵件/聯繫人。 我能夠創建Office 365試用訂閱並註冊我的應用程序。因此,現在我可以訪問我的應用的終端URL,客戶端ID和客戶端密鑰。 這裏是我的代碼:無法使用NodeJS/node-outlook訪問Office 365
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="[email protected]*", 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」屬性。錯誤是一樣的。
那麼首先,我可以使用自行頒發的PKI證書嗎? PKI證書的要求是什麼,以便它們可以與Azure AD一起使用?我使用SHA1算法和2048位加密。這些夠了嗎?
而且我看了一下簡單的oauth2庫的源代碼,發現「CA」是一個可以使用的PKI設置的唯一選擇。它被明確檢查。所有其他與PKI有關的nodejs https選項(cert,key,passphrase,...)都會被忽略,永遠不會獲得實際的請求代碼。
我是唯一遇到問題的人嗎?
謝謝你的及時迴應,傑森。這將是定期運行在雲(不是Azure)上的集成腳本。它應該與我們的系統同步Office 365數據。所以如果我理解正確,應用程序類型是守護進程/服務。沒有Web組件。 你確定clientSecret不是必需的嗎? Azure AD將其與客戶端ID和訪問端點一起發給我。 – Dmitriy
有沒有辦法使用clientSecret而不是證書。 Azure AD正在創建安全通道,因此邏輯上爲什麼我需要一些額外的證書來標識自己,如果我已經擁有clientID,clientSecret和唯一端點? – Dmitriy
不,Exchange在使用應用程序權限/客戶端憑證流時需要證書斷言。這是爲了防止有人獲得您的客戶ID和祕密,並獲得對所有同意您的應用的組織的全球訪問權。您可以爲此使用自簽名證書。我會看看我是否可以拿出一個示例腳本。 –