【问题标题】:Get all messages by label from gmail account从 gmail 帐户按标签获取所有消息
【发布时间】:2014-12-31 19:53:51
【问题描述】:

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

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

Labels

Messages

我该如何使用 javascript,我需要包含哪些 js 库,如何使用 google Api?我以前从未使用过它,所以任何人都可以给我一些简单的完整示例吗?

【问题讨论】:

    标签: javascript php gmail gmail-api


    【解决方案1】:

    这是一个完整示例,展示了如何加载 Google APIs Javascript 客户端、加载 gmail API 以及调用这两个 API 方法来列出标签和收件箱消息。对于每个 API 调用,Gmail lAPI 文档中有很多 javascript 代码 sn-ps,因此您可以将下面代码的结构与任何特定代码 sn-p 结合起来,以实现您想要的。

    <!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>

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2018-09-29
      • 1970-01-01
      • 2015-11-02
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多