从gmail帐户按标签获取所有邮件

时间:2014-11-04 19:27:31

标签: javascript php gmail gmail-api

我想创建一个基于google gmail api导出的报告工具。 所以我想要做的主要是检索,通过我的帐户在gmail中的标签获取所有收件箱邮件,并在我的自定义ehtml文档中的自定义结构中显示它。 我想用php或javascript做。 我已经在Google API上做了一些研究,但无法理解如何从哪里开始工作?

我认为如果能从这个网址获取JSON数据会很好

Labels

Messages

我怎么能用javascript做什么,我需要包含哪些js libs,如何使用google Api?我之前从未使用过它,所以有人能给我看一些简单的完整例子吗?

1 个答案:

答案 0 :(得分:2)

以下是一个完整的示例,展示了如何加载Google API Javascript客户端,加载gmail API以及调用两种API方法来列出标签和收件箱邮件。每个API调用的Gmai lAPI文档中都有很多javascript代码片段,因此您可以将下面代码的结构与任何特定的代码片段结合起来,以实现您的目标。

<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8' />
  </head>
  <body>
    <!--Add a button for the user to click to initiate auth sequence -->
    <button id="authorize-button" style="visibility: hidden">Authorize</button>
    <div id="content"></div>
    <p>Test gmail API.</p>
    <script type="text/javascript">
      // Enter a client ID for a web application from the Google Developer Console.
      // In your Developer Console project, add a JavaScript origin that corresponds to the domain
      // where you will be running the script.
      var clientId = 'YOUR_CLIENT_ID_HERE';

      // To enter one or more authentication scopes, refer to the documentation for the API.
      var scopes = 'https://www.googleapis.com/auth/gmail.readonly';

      // Use a button to handle authentication the first time.
      function handleClientLoad() {
        window.setTimeout(checkAuth,1);
      }

      function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
      }

      function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;
      }

      // Load the API and make an API call.  Display the results on the screen.
      function makeApiCall() {
        gapi.client.load('gmail', 'v1', function() {
          listLabels();
          listMessages();
        });
      }

      /**
       * Get all the Labels in the authenticated user's mailbox.
       */
      function listLabels() {
        var userId = "me";
        var request = gapi.client.gmail.users.labels.list({
          'userId': userId
        });
        request.execute(function(resp) {
          var labels = resp.labels;
          var output = ("<br>Query returned " + labels.length + " labels:<br>");
          for(var i = 0; i < labels.length; i++) {
            output += labels[i].name + "<br>";
          }
          document.getElementById("content").innerHTML += output;
        });
      }

      /**
       * Get all the message IDs in the authenticated user's inbox.
       */
      function listMessages() {
        var userId = "me";
        var request = gapi.client.gmail.users.messages.list({
          'userId': userId
        });
        request.execute(function(resp) {
          var messages = resp.messages;
          var output = "<br>Query returned " + messages.length + " messages:<br>";
          for(var i = 0; i < messages.length; i++) {
            output += messages[i].id + "<br>";
          }
          document.getElementById("content").innerHTML += output;
        });
      }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
  </body>
</html>

相关问题