检索最近5条未读消息

时间:2014-08-13 23:03:43

标签: javascript jquery gmail gmail-api

我正在尝试使用带有javascript的GMAIL API来检索用户收件箱中的未读邮件。最多只有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,从那里我很好。

1 个答案:

答案 0 :(得分:6)

目前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>