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

标签 javascript jquery gmail-api

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

我没有收到任何错误,我得到了输出,只是看起来像没有值的键。

这是控制台输出的样子:enter image description here

代码如下:

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 中的 GMail api 没有明确的方法来访问特定的电子邮件部分 - 收件人/发件人/等等。 Java 中的 GMail api 具有此功能。 Javascript 中的 Gmail api 仍处于测试阶段。 api list

你还想做:这是大纲:

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

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

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

困难...你打赌...:)

关于javascript - 在用户的 GMail 收件箱中获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25353304/

相关文章:

javascript - 客户端和服务器端编程有什么区别?

javascript - 当 jQuery 选择中未选择任何内容时,表单提交上的空数组

python - 使用 GmailAPI 在帐户设置中添加自动转发时出现问题

python - 使用 Gmail API 发送回复到电子邮件线程

javascript - 如何从javascript中的字符串中提取id?

javascript - 尝试使 div 从页面顶部向下和向上滑动使用 - 顶部定位和点击时的 jquery 动画

javascript - xterm.js - 获取当前行文本

jquery - 可以使用 jQuery 进行原生 react

javascript - jQuery 变量未在动态创建的文本框中更新

android - 如何使用 Java 中的 Gmail API 导入邮件