首页 > 解决方案 > 我无法通过 gmail-api 中的 id 检索邮件

问题描述

所以我用两个方法checkInbox() 和getMail() 创建了一个类Check。

checkInbox() 检索所有邮件,getMail(msgId) 将检索带有输入 ID 的邮件。

checkInbox() 工作正常,但 getMail() 方法提供与 checkInbox() 相同的输出,即列出所有邮件而不是返回请求的邮件。

class Check{

    constructor(auth){
        this.me = 'mygmailid';
        this.gmail = google.gmail({version: 'v1', auth});
        this.auth = auth;
    }

    checkInbox(){
        this.gmail.users.messages.list({
            userId: this.me
        }, (err, res) => {
            if(!err){
                console.log(res.data);
            }
        })
    }

    getMail(msgId){
        this.gmail.users.messages.get({
            'userId': this.me,
            'id': msgId
        }, (err, res) => {
            if(!err){
                console.log(res);
            }
        });
    }

}

var obj = new dem(auth);
obj.getMail('someRandomIdNumber');

我没有附上授权码,因为它工作正常。导入和导出类也没有错误。

标签: javascriptnode.jsecmascript-6google-apigmail-api

解决方案


这正是获取 gmail 消息应该执行的操作:

  const response = await this.gmail.users.messages.get({
    auth: this.oAuth2Client,
    userId: this.gmailAddress,
    id: messageId,
    format: 'full'
  })

你可以在这里找到整个实现。它是 Gmail-API 的轻量级包装器


推荐阅读