首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gmail-API Threads.list不返回响应中的消息。

Gmail-API Threads.list不返回响应中的消息。
EN

Stack Overflow用户
提问于 2020-08-20 01:17:41
回答 1查看 339关注 0票数 1

我试图使用Gmail从用户那里获取所有线程,并使用threads.list获得该线程中的所有消息。在gmail文档中,它指出以下是到达此端点后的响应:

代码语言:javascript
复制
{
 "threads": [
   users.threads Resource
 ],
 "nextPageToken": string,
 "resultSizeEstimate": unsigned integer
}

我有一个简单的函数可以到达这个端点。

代码语言:javascript
复制
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替换了实际的电子邮件,并替换了我的隐私标记):

代码语言:javascript
复制
{
    "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属性完全丢失。如有任何指导,我将不胜感激。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-20 02:23:29

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

修改脚本:

代码语言:javascript
复制
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检索电子邮件。

参考文献:

-Users.threads:列表 -Users.threads:获取

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63497021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档