【问题标题】:Gmail-API Threads.list is not returning messages in the responseGmail-API Threads.list 未在响应中返回消息
【发布时间】:2020-12-09 07:33:35
【问题描述】:

我正在尝试使用 Gmail-API 从用户那里获取所有线程,并通过使用 threads.list 来获取该线程中的所有消息。在 gmail 文档中,它指出以下是点击此端点的响应:

{
 "threads": [
   users.threads Resource
 ],
 "nextPageToken": string,
 "resultSizeEstimate": unsigned integer
}

我有一个简单的函数来命中这个端点

const {google} = require('googleapis');

/**
 * Lists the threads in the user's account.
 *
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */

export function listThreads(auth,fn) {
    const gmail =  google.gmail({version: 'v1', auth});

    gmail.users.threads.list({
      userId: 'me',
      q: 'to:abc@abc.com '
    }, (err, res) => {
      if (err) throw 'The API returned an error: ' + err;
      
      // fn is a callback used to return data to the handler since
      // the res object is in the callback of thread.list
      fn(res)
    });

  }

以下是我得到的回复(我用 abc 替换了实际的电子邮件,并替换了我的隐私令牌):

{
    "gmail": {
        "config": {
            "url": "https://www.googleapis.com/gmail/v1/users/me/threads?q=to%3Aabc%40abc.com",
            "method": "GET",
            "headers": {
                "Accept-Encoding": "gzip",
                "User-Agent": "google-api-nodejs-client/0.7.2 (gzip)",
                "Authorization": "Bearer 123456",
                "Accept": "application/json"
            },
            "params": {
                "q": "to:abc@abc.com"
            },
            "responseType": "json"
        },
        "data": {
            "threads": [
                {
                    "id": "173bf0efdd1f1dc4",
                    "snippet": "Hello abc, Attached are the screenshots of my website for the requirements. Please send me an email with all of the information I'm asking for in the forms. For the colors, here is the site to",
                    "historyId": "4759550"
                }
            ],
            "resultSizeEstimate": 1
        },
        "headers": {
            "alt-svc": "h3-29=\":443\"; ma=2592000,h3-27=\":443\"; ma=2592000,h3-T050=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"",
            "cache-control": "private",
            "connection": "close",
            "content-encoding": "gzip",
            "content-type": "application/json; charset=UTF-8",
            "date": "Thu, 20 Aug 2020 01:13:07 GMT",
            "server": "ESF",
            "transfer-encoding": "chunked",
            "vary": "Origin, X-Origin, Referer",
            "x-content-type-options": "nosniff",
            "x-frame-options": "SAMEORIGIN",
            "x-xss-protection": "0"
        },
        "status": 200,
        "statusText": "OK"
    }
}

如您所见, res.data.threads 的 messages 属性完全丢失了。如有任何指导,我将不胜感激。

谢谢

【问题讨论】:

标签: javascript node.js gmail-api


【解决方案1】:

在这种情况下,为了从每个线程中检索所有消息,需要使用“Users.threads:get”的方法。当你的脚本被修改后,变成如下。

修改脚本:

const gmail =  google.gmail({version: 'v1', auth});

gmail.users.threads.list(
  {
    userId: "me",
    q: "to:abc@abc.com ",
  },
  (err, res) => {
    if (err) throw "The API returned an error: " + err;
    Promise.all(
      res.data.threads.map(({ id }) => {
        return new Promise((resolve, reject) => {
          gmail.users.threads.get({ userId: "me", id }, (err, res) => {
            if (err) rejects(err);
            resolve(res.data);
          });
        });
      })
    ).then((res) => {
      fn(res);
    });
  }
);

注意:

  • 在这个修改后的脚本中,它假设您已经能够使用 Gmail API 检索电子邮件。

参考资料:

-Users.threads: list -Users.threads: get

【讨论】:

  • 这行得通!我一定在文档中错过了这一点。我认为消息应该与响应一起出现。
  • @ajohnson10209 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
相关资源
最近更新 更多