2014-11-04 74 views
0

我想創建一個基於google gmail api導出的報告工具。 所以我想要做的主要事情是檢索,從我的帳戶在Gmail中獲取標籤中的所有收件箱郵件,並將其顯示在我自定義的ehtml文檔中的自定義結構中。 我想用php或javascript來做。 我在Google API上做了一些研究,但無法理解如何開始工作,從哪裏開始?從gmail帳戶獲取所有郵件標籤

我認爲這將是很好,如果能得到JSON數據從該網址

Labels

Messages

我怎麼能做到這一點與JavaScript,我需要什麼樣的js庫,包括如何與谷歌Api工作?我從來沒有使用它,所以任何人都可以給我看一個簡單的完整例子嗎?

回答

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>

相關問題