【问题标题】:retrieve last 5 unread messages检索最后 5 条未读消息
【发布时间】:2014-10-07 11:43:24
【问题描述】:

我正在尝试使用带有 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,从那里我很好。

【问题讨论】:

  • 您在尝试实际使用 API 之前是否对会话进行了身份验证?
  • @Stormie 我只在 javascript 中使用 G+ api 登录。我还需要做些什么吗?
  • 实际上没有任何 JavaScript 绑定(由 Google 提供),Java, Python and .net 有。我可以用这些语言提供解决方案,但不能用 JavaScript。
  • @Stormie 但是代码返回 JSON,我可以用 javascript/jquery 解析它。所以我不能把它翻译成使用 jQuery 的 $.get 和 $.post 的 get 和 post 请求吗?
  • 那么我将无法为您提供所有帮助,值得编辑您的问题以包含json 标签:)

标签: javascript jquery gmail gmail-api


【解决方案1】:

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

【讨论】:

  • 非常感谢,但在控制台中,我没有收到电子邮件?控制台中的输出如下所示:Object \n historyId: "4414613" \n id: "147dd2015d281d8f" \n snippet: "" \n __proto__: \n Object \n 147dd2015d281d8f \n Object \n 147b4020916928f0 消息或至少部分消息不应该在 sn-p 中吗?
  • 如果您能帮助我,我将不胜感激。这是一个实时示例,您可以自己检查控制台输出:irfandesign.com/gmail.html
  • threads.list方法只返回每个线程的historyId和id。您必须使用 threads.get 来获得更详细的响应。目前threads.list有一个空的sn-p字段,这个字段将来可能会被使用甚至删除,所以暂时不要依赖它。
  • 谢谢!我真的很感谢你的帮助。如果你能帮我解决这个问题,我很乐意设置赏金并将其提供给你。当我 console.log 响应 gapi.client.gmail.users.threads.get 时,我收到 400 错误“'需要 ID'”。我是否需要使用 gapi.client.gmail.users.theads.list 获取线程的 id,然后将其传递给 threads.get ?只是在请求的选项中?如果没有,我该怎么做?请在评论中提及我,以便我在希望您回复时收到通知。
  • @user3743069 你是对的,通常的流程是调用threads.list,然后使用响应中的线程ID来调用threads.get。在进行 threads.get 调用时,您需要 userId 和线程的 id。查看文档以获取更多详细信息developers.google.com/gmail/api/v1/reference/users/threads/get
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
相关资源
最近更新 更多