javascript - 检索最后 5 条未读消息

标签 javascript jquery gmail gmail-api

我正在尝试将 GMAIL API 与 javascript 结合使用来检索用户收件箱中的未读邮件。最多最后 5 个。

在用户使用 G+ api 登录后,我尝试使用 jQuery 的 $.get,但我在控制台中收到 404 错误。

这是我正在运行的:$.get('https://www.googleapis.com/gmail/v1/users/me/', function(data){ console.log(data); }); 我没有尝试将消息限制为未读或最多 5 条,因为目前我什至无法检索任何消息。

如何获取用户收件箱中的未读消息,最多5条,甚至获取当前登录用户收件箱中的消息?

我想了解如何从 API 获取 JSON。我可以自己解析,但是发送什么请求(如何获取用户id和线程id)然后获取JSON。 请帮我弄清楚如何在浏览器或控制台中查看 JSON,从那里我没问题。

最佳答案

目前 Gmail API 文档中没有 JavaScript 快速入门,但类似这样的东西应该可以帮助您入门,它列出了用户收件箱中线程的第一页。请记住将 YOUR_CLIENT_ID 替换为您在开发者控制台中的实际客户端 ID。

<html>
  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <script type="text/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);
      }

    </script>
    <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </head>
  <body>
    <input type="button" id="authorizeButton" style="display: none" value="Authorize" />
    <p id="notice" style="display: none">check browser console for output</p>
  </body>
</html>

关于javascript - 检索最后 5 条未读消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25297607/

相关文章:

javascript - 即使使用命名空间后也无法附加到 SVG

javascript - Jquery 滑出菜单

javascript - 如何将多个事件(鼠标悬停/鼠标移出)添加到一个选择器中

javascript - WordPress:在搜索中添加下拉菜单?

jquery - 使用 :before via Jquery

javascript - 在 ckeditor 中编辑 Div - 无法编辑内部的

Android - 让 GMailSender 工作有问题吗?

javascript - 有没有人找到一种愉快地处理 WYSIHTML5 代码的方法?

java - 如何以安全的方式通过 Gmail SMTP 发送电子邮件

email - 使用 smtp 发送电子邮件并设置 message-id