简体   繁体   English

我无法通过 gmail-api 中的 id 检索邮件

[英]I am unable to retrieve the message by its id in gmail-api

So I have created a class Check with two methods checkInbox() and getMail().所以我用两个方法 checkInbox() 和 getMail() 创建了一个类 Check。

The checkInbox() retrieves all the mails and the getMail(msgId) will retrive the mail with the input Id. checkInbox() 检索所有邮件,getMail(msgId) 将检索带有输入 Id 的邮件。

The checkInbox() works fine but the getMail() method gives the same output as checkInbox() ie lists all the mails instead of returning the requested mail. 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');

I haven't attached the authorization code as it works fine.我没有附上授权码,因为它工作正常。 Also there is no error in importing and exporting the class.导入和导出类也没有错误。

This is exactly what you should do to fetch a gmail-message:这正是获取 gmail 消息应该做的事情:

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

you can find the whole implementation here .你可以在这里找到整个实现。 It's a light wrapper on Gmail-APIs它是 Gmail-API 上的一个轻量级包装器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM