javascript - 从 Graph 获取单个消息

标签 javascript node.js microsoft-graph-api

我正在尝试从 Office 365 邮箱获取一封电子邮件。

我通过 POST (req.body.id) 将电子邮件 id 发送到我的应用程序,然后调用此代码以获取一些电子邮件属性:

router.post('/id', async function(req, res, next) {
    console.log("email with ID -> ", req.body.id)
    let parms = { title: 'Inbox', active: { inbox: true } };
    const accessToken = await authHelper.getAccessToken(req.cookies, res);
    const userName = req.cookies.graph_user_name;

    if (accessToken && userName) {
    parms.user = userName;
    // Initialize Graph client
    const client = graph.Client.init({
        authProvider: (done) => {
        done(null, accessToken);
        }
    });

    try {
        const result = await client
        .api('/me/messages/', req.body.id)
        .select('id,subject,from,toRecipients,ccRecipients,body,sentDateTime,receivedDateTime')
        .get();

        parms.messages = result.value;
        console.log("email -> ", result.value);
        res.render('message', parms);

    } catch (err) {
        parms.message = 'Error retrieving messages';
        parms.error = { status: `${err.code}: ${err.message}` };
        parms.debug = JSON.stringify(err.body, null, 2);
        res.render('error', parms);
        }

    } else {
    // Redirect to home
    res.redirect('/');
    }
});

目前,result.value 包含邮箱中的所有邮件,而不仅仅是具有提供的 id 的邮件。

有人能告诉我我的错误在哪里吗?

最佳答案

api 方法有一个路径参数。像 .api('/me/messages/', req.body.id) 这样调用它是有效地向它发送一个路径 ("/me/messages/") 以及一个它忽略的附加参数。

您需要向它发送一个字符串,因此您需要将 req.body.id 附加到路径 ({path} + {id}) :

const result = await client
  .api('/me/messages/' + req.body.id)
  .select('id,subject,from,toRecipients,ccRecipients,body,sentDateTime,receivedDateTime')
  .get();

关于javascript - 从 Graph 获取单个消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55796029/

相关文章:

javascript - 具有轮询周期的不同 ajax 调用

javascript - 如何使用 jQuery 和 dataTables 进行数字排序?

javascript - 丰富 :jQuery not working on JS call

azure - AAD B2C : Returning value: [] when trying to filter by identities

excel - 访问 Excel 工作簿返回 "The resource could not be found."

javascript - 在javascript中用连字符分割字符串

node.js - Symfony 和 Socket.io

node.js - Node.js 如何在其内部实现 require() ?

javascript - Moment.js 24小时时间格式,处理24小时

authentication - 无需注册应用程序即可通过 REST 进行 Office 365 身份验证