2

我正嘗試使用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位加密。這些夠了嗎?

這是我作爲一個手冊:http://blogs.msdn.com/b/exchangedev/archive/2015/01/21/building-demon-or-service-apps-with-office-365-mail-calendar-and-contacts-apis-oauth2-client-credential-flow.aspx

而且我看了一下簡單的oauth2庫的源代碼,發現「CA」是一個可以使用的PKI設置的唯一選擇。它被明確檢查。所有其他與PKI有關的nodejs https選項(cert,key,passphrase,...)都會被忽略,永遠不會獲得實際的請求代碼。

我是唯一遇到問題的人嗎?

回答

3

該錯誤是因爲在auth請求中使用祕密而不是cert。就你而言,你根本不想使用祕密。這聽起來像是真正的問題在於,如果simple-oauth2庫會處理將auth請求轉換爲Azure期望的格式。格式的細節如下:Office 365 Rest API - Daemon week authentication

我查看了自述文件simple-oauth2,他們的客戶端憑證流示例使用祕密而不是基於證書的斷言。看看配置代碼,我沒有看到在那裏做任何基於證書的認證的能力,所以這個庫可能不適用於這種情況(除非我錯過了它)。

更新:好消息是,adal-node library不支持使用證書,而且它使用起來相當簡單。棘手的部分是獲得證書。

我已經開始了一個示例Node.js腳本,您可以在這裏找到它:https://github.com/jasonjoh/node-service。到目前爲止,它只使用來自Azure的證書檢索令牌。自述文件包含了我通過的所有步驟來獲取證書。

+0

謝謝你的及時迴應,傑森。這將是定期運行在雲(不是Azure)上的集成腳本。它應該與我們的系統同步Office 365數據。所以如果我理解正確,應用程序類型是守護進程/服務。沒有Web組件。 你確定clientSecret不是必需的嗎? Azure AD將其與客戶端ID和訪問端點一起發給我。 – Dmitriy

+0

有沒有辦法使用clientSecret而不是證書。 Azure AD正在創建安全通道,因此邏輯上爲什麼我需要一些額外的證書來標識自己,如果我已經擁有clientID,clientSecret和唯一端點? – Dmitriy

+0

不,Exchange在使用應用程序權限/客戶端憑證流時需要證書斷言。這是爲了防止有人獲得您的客戶ID和祕密,並獲得對所有同意您的應用的組織的全球訪問權。您可以爲此使用自簽名證書。我會看看我是否可以拿出一個示例腳本。 –

相關問題