![](/img/trans.png)
[英]Microsoft Graph Mail API with $search filter not working when we check received time is greater
[英]Microsoft Graph API - AuthenticationError when accessing mail
我目前正在實施一個 Outlook 插件,它應該可以訪問某些郵件字段。 因此,我設置了一個啟動項目並在 Microsoft Azure Active Directory 設置中設置了委派的 Mail.Read 權限,並將其添加到我的 scope 配置中。 但是,當我發送請求時
graph.microsoft.com/v1.0/me/messages/
我得到這個作為回應:
Error: Bad Request
at IncomingMessage.<anonymous> (C:\Users\zz\public\javascripts\odata-helper.js:53:29)
at IncomingMessage.emit (events.js:322:22)
at endReadableNT (_stream_readable.js:1187:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 400,
bodyCode: 'AuthenticationError',
bodyMessage: 'Error authenticating with resource'
}
我不明白為什么會出現此錯誤,因為用戶身份驗證有效並且我對其他圖形端點沒有任何問題(例如,從/me/drive/root/children
檢索我的 OneDrive 文件列表)
我將其用作代碼庫: https://docs.microsoft.com/en-us/office/dev/add-ins/develop/create-sso-office-add-ins-nodejs
我沒有重現您的問題,並且graph.microsoft.com/v1.0/me/messages
在您共享的示例中工作正常。
請參考我的配置。
除了文檔中提到的配置之外,我還做了以下更改。
在 Azure AD 應用程序中添加Mail.Read
Delegated 權限。
修改app.js
文件中的 Microsoft Graph 終結點。 請注意,我正在查詢消息的subject
屬性。 所以我修改了兩個地方: $select=subject&$top=10
和itemNames.push(item['subject'])
。
app.get('/getuserdata', async function(req, res, next) {
const graphToken = req.get('access_token');
const graphData = await getGraphData(graphToken, "/me/messages", "?$select=subject&$top=10");
if (graphData.code) {
next(createError(graphData.code, "Microsoft Graph error " + JSON.stringify(graphData)));
}
else
{
const itemNames = [];
const oneDriveItems = graphData['value'];
for (let item of oneDriveItems){
itemNames.push(item['subject']);
}
res.send(itemNames)
}
}
在加載項清單文件“manifest\manifest_local.xml”中,我添加了 scope“Mail.Read”。
<WebApplicationInfo>
<Id>a7eb1d00-f724-4799-8a46-809f2dba63f8</Id>
<Resource>api://localhost:44355/a7eb1d00-f724-4799-8a46-809f2dba63f8</Resource>
<Scopes>
<Scope>Files.Read.All</Scope>
<Scope>profile</Scope>
<Scope>Mail.Read</Scope>
</Scopes>
</WebApplicationInfo>
在文件routes\authRoute.js
中,我添加了“Mail.Read”,如下所示:
else {
const [schema, jwt] = authorization.split(' ');
const formParams = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: jwt,
requested_token_use: 'on_behalf_of',
scope: ['Files.Read.All', 'Mail.Read'].join(' ')
};
這些都是配置,我可以在 Microsoft Word 插件中獲取消息的主題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.