【问题标题】:Get messages in users' GMail inbox在用户的 GMail 收件箱中获取邮件
【发布时间】:2014-10-10 18:37:38
【问题描述】:

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

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

控制台输出如下所示:

代码如下:

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

**更新:我目前正在使用的内容:**

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”。 当我记录 dataItem.value 时,我得到一个 dataMessage.messages 未定义,并且不能有 0 的索引。

非常感谢帮助您完成这项工作!

【问题讨论】:

    标签: javascript jquery gmail-api


    【解决方案1】:

    Javascript 中的 GMail api 没有明确的方法来访问特定的电子邮件部分 - 到/从/等。 Java 中的 GMail api 具有此功能。 Javascript 中的 Gmail api 仍处于测试阶段。 api list

    你还想这样做:这里是大纲:

    获取消息列表而不是获取线程列表: message list

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

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

    很难...你敢打赌... :)

    【讨论】:

    • 你能告诉我如何获取线程列表吗?通过我目前正在做的事情,我在控制台中获得了 id。但是我如何从那里获取消息?这是一个活生生的例子:irfanknow.com/gmail.html
    【解决方案2】:

    就像 Amit 上面所说的,您可以使用 messages.list() 来获取消息 ID 列表。有了这些,您可以简单地调用messages.get(),它会以parsed 形式返回一封电子邮件,您可以通过message.payload.headers 访问标题。您不需要对“原始”消息进行 base64 编码。

    【讨论】:

      【解决方案3】:

      这是我从邮件中获取来自电子邮件 ID 的操作
      调用listThread() 方法后,我调用getThread() 方法从该线程中获取来自电子邮件的ID,如下所示。

      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 Format for the message

      【讨论】:

      • 你的整个代码是什么样的?我正在尝试您上面的内容,但在控制台中仍然一无所获。这是我所有代码的样子:gist.github.com/theirf/f142d3b02e5e2343403a我做错了什么?
      • 好吧,这更有意义。我运行它并收到string is not a function 的错误,但后来我从listThreads 中删除了第二个参数,我没有收到任何错误。除了现在,我再次在控制台中一无所获。没有错误或结果。这是我所拥有的:gist.github.com/theirf/71464ac6e9ead6425074
      • @user3743069 在 chrome 或 FireFox 中使用 F12 调试您的代码,检查您是否得到正确的响应...
      • @user3743069 有没有添加getThreads() 方法,没有的话检查gist.github.com/theirf/71464ac6e9ead6425074,我已经添加了方法
      • 谢谢。现在,我正在使用您的 getThread() 函数,但出现错误。它说.messages 未定义?并且 .messages 没有索引 0,因为它是未定义的。这是代码:gist.github.com/theirf/71464ac6e9ead6425074
      猜你喜欢
      • 2019-10-24
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2015-03-13
      • 2011-05-30
      • 2015-04-07
      相关资源
      最近更新 更多