从gmail帐户按标签获取所有消息


Get all messages by label from gmail account

我想创建一个基于从googlegmailapi导出的报告工具。因此,我想做的主要事情是检索,从我在gmail中的帐户中按标签获取所有收件箱消息,并在我的自定义ehtml文档中以自定义结构显示它。我想用php或javascript来实现。我对谷歌API做了一些研究,但不明白如何开始工作,从哪里开始?

我认为如果能从这个url 中获得JSON数据会很好

标签

消息

如何使用javascript,我需要包含哪些js-libs,如何使用googleApi?我以前从未使用过它,所以有人能给我举一些简单的完整例子吗?

下面是一个完整的示例,展示了如何加载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>