gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:50:06 30 4
gpt4 key购买 nike

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

我想如果能从这个url获取JSON数据就好了

Labels

Messages

我如何使用 javascript 做到这一点,我需要包含哪些 js 库,如何使用 google Api?我以前从未使用过它,所以有人可以向我展示一些简单的完整示例吗?

最佳答案

这里是一个完整的示例,展示了如何加载 Google API Javascript 客户端、加载 gmail API 以及调用两个 API 方法来列出标签和收件箱消息。 Gmai lAPI 文档中针对每个 API 调用都有大量 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>

关于javascript - 按标签从 Gmail 帐户获取所有邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743377/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com