首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在用户的GMail收件箱中获取消息

在用户的GMail收件箱中获取消息
EN

Stack Overflow用户
提问于 2014-08-17 20:21:54
回答 3查看 5K关注 0票数 5

在下面的代码中,我将登录,授权应用程序,并通过GMail API获得控制台输出。我相信我正在获得线程和线程in,但我没有看到控制台中的消息。

我没有得到任何错误,我得到了输出,就像没有值的键。

以下是控制台输出的样子:

以下是代码:

代码语言:javascript
运行
复制
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var USER = 'me';

  /**
   * Called when the client library is loaded to start the auth flow.
   */
  function handleClientLoad() {
    window.setTimeout(checkAuth, 1);
  }

  /**
   * Check if the current user has authorized the application.
   */
  function checkAuth() {
    gapi.auth.authorize(
        {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
        handleAuthResult);
  }

  /**
   * Called when authorization server replies.
   *
   * @param {Object} authResult Authorization result.
   */
  function handleAuthResult(authResult) {
    var authButton = document.getElementById('authorizeButton');
    var outputNotice = document.getElementById('notice');
    authButton.style.display = 'none';
    outputNotice.style.display = 'block';
    if (authResult && !authResult.error) {
      // Access token has been successfully retrieved, requests can be sent to the API.
      gapi.client.load('gmail', 'v1', function() {
        listThreads(USER, function(resp) {
          var threads = resp.threads;
          for (var i = 0; i < threads.length; i++) {
            var thread = threads[i];
            console.log(thread);
            console.log(thread['id']);
          }
        });
      });
    } else {
      // No access token could be retrieved, show the button to start the authorization flow.
      authButton.style.display = 'block';
      outputNotice.style.display = 'none';
      authButton.onclick = function() {
          gapi.auth.authorize(
              {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
              handleAuthResult);
      };
    }
  }


  /**
   * Get a page of Threads.
   *
   * @param  {String} userId User's email address. The special value 'me'
   * can be used to indicate the authenticated user.
   * @param  {Function} callback Function called when request is complete.
   */
  function listThreads(userId, callback) {
    var request = gapi.client.gmail.users.threads.list({
      'userId': userId
    });
    request.execute(callback);
  }

如何检索消息的发件人地址、主题和正文?使用js中的GMAIL API

**最新情况:我目前正在从事的工作:**

代码语言:javascript
运行
复制
listThreads('me', function(dataResult){
    $.each(dataResult, function(i, item){
        getThread('me', item.id, function(dataMessage){
            console.log(dataMessage);
            var temp = dataMessage.messages[0].payload.headers;
            $.each(temp, function(j, dataItem){
                if(dataItem.name == 'From'){
                    console.log(dataItem.value);
                }
            });
         });
      });
   });

当我记录dataMessage时,我会得到一个400个错误,“id required”。当我记录dataItem.value时,我得到一个dataMessage.messages是未定义的,并且不能有0的索引。

我将非常感谢帮助使这一工作!

EN

回答 3

Stack Overflow用户

发布于 2014-08-18 16:24:24

Javascript中的GMail api没有明确的方法来访问特定的电子邮件部分/从/等。Java中的GMail api具有这个特性。Javascript中的Gmail仍然在Beta中。api列表

你还想这么做:这是提纲:

不是获取线程列表,而是获取消息列表:讯息列表

解析从上一次调用中检索到的json消息id,将其与以下内容一起使用:讯息获取

获取以URL编码的base64格式的原始消息。解码和解析。安全编码 编码

很难..。你当然..。:)

票数 1
EN

Stack Overflow用户

发布于 2014-09-05 07:33:08

这里是我从电子邮件id 从message获得的过程

调用listThread()方法之后,我调用getThread()方法从该线程获取电子邮件ID,如下所示。

代码语言:javascript
运行
复制
listThreads("me", "", function (dataResult) {
         $.each(dataResult, function (i, item) {
           getThread("me", item.id, function (dataMessage) {
             var temp = dataMessage.messages[0].payload.headers;
             $.each(temp, function (j, dataItem) {
                   if (dataItem.name == "From") {
                       Console.log(dataItem.value);
                    }
             });
        });
    });
});

类似地,您可以从消息中获取其他详细信息。

参考:消息的JSON格式

票数 1
EN

Stack Overflow用户

发布于 2014-09-05 16:06:20

正如上面Amit所说,您可以使用messages.list()来获取消息ids的列表。通过这些方法,您可以简单地调用messages.get(),这将以解析的形式返回一封电子邮件,您可以通过message.payload.headers到达标头。您不需要获得“原始”消息base64编码。

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

https://stackoverflow.com/questions/25353304

复制
相关文章

相似问题

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