在用户的GMail收件箱中获取邮件

wor*_*ith 5 javascript jquery gmail-api

在下面的代码中,我登录,授权应用程序并通过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);
  }
Run Code Online (Sandbox Code Playgroud)

如何检索邮件的发件人地址,主题和正文?在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);
                }
            });
         });
      });
   });
Run Code Online (Sandbox Code Playgroud)

当我记录dataMessage时,出现400错误,“需要ID”。当我记录dataItem.value时,我得到一个dataMessage.messages是未定义的,并且不能具有0的索引。

我非常感谢您的帮助,以使它正常工作!

Ami*_*t G 1

Javascript 中的 GMail api 没有显式方法来访问特定电子邮件部分 - 发送/发件人/等。Java 中的 GMail api 具有此功能。Javascript 中的 Gmail api 仍处于测试阶段。接口列表

你仍然想这样做:这是大纲:

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

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

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

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