繁体   English   中英

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

[英]Get all messages by label from gmail account

我想基于Google gmail API的导出创建报告工具。 因此,我主要要做的是从gmail帐户中检索标签中的所有收件箱邮件,并在自定义ehtml文档中以自定义结构显示它。 我想用php或javascript做。 我已经在Google API上进行了一些研究,但不知道如何从哪里开始工作?

我认为如果可以从此网址获取JSON数据会很好

标签

留言内容

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM