gpt4 book ai didi

javascript - 检索最后 5 条未读消息

转载 作者:行者123 更新时间:2023-11-29 16:11:54 25 4
gpt4 key购买 nike

我正在尝试将 GMAIL API 与 javascript 结合使用来检索用户收件箱中的未读邮件。最多最后 5 个。

在用户使用 G+ api 登录后,我尝试使用 jQuery 的 $.get,但我在控制台中收到 404 错误。

这是我正在运行的:$.get('https://www.googleapis.com/gmail/v1/users/me/', function(data){ console.log(data); }); 我没有尝试将消息限制为未读或最多 5 条,因为目前我什至无法检索任何消息。

如何获取用户收件箱中的未读消息,最多5条,甚至获取当前登录用户收件箱中的消息?

我想了解如何从 API 获取 JSON。我可以自己解析,但是发送什么请求(如何获取用户id和线程id)然后获取JSON。请帮我弄清楚如何在浏览器或控制台中查看 JSON,从那里我没问题。

最佳答案

目前 Gmail API 文档中没有 JavaScript 快速入门,但类似这样的东西应该可以帮助您入门,它列出了用户收件箱中线程的第一页。请记住将 YOUR_CLIENT_ID 替换为您在开发者控制台中的实际客户端 ID。

<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script type="text/javascript">
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var USER = 'me';

/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}

/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}

/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authButton = document.getElementById('authorizeButton');
var outputNotice = document.getElementById('notice');
authButton.style.display = 'none';
outputNotice.style.display = 'block';
if (authResult && !authResult.error) {
// Access token has been successfully retrieved, requests can be sent to the API.
gapi.client.load('gmail', 'v1', function() {
listThreads(USER, function(resp) {
var threads = resp.threads;
for (var i = 0; i < threads.length; i++) {
var thread = threads[i];
console.log(thread);
console.log(thread['id']);
}
});
});
} else {
// No access token could be retrieved, show the button to start the authorization flow.
authButton.style.display = 'block';
outputNotice.style.display = 'none';
authButton.onclick = function() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false},
handleAuthResult);
};
}
}


/**
* Get a page of Threads.
*
* @param {String} userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @param {Function} callback Function called when request is complete.
*/
function listThreads(userId, callback) {
var request = gapi.client.gmail.users.threads.list({
'userId': userId
});
request.execute(callback);
}

</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</head>
<body>
<input type="button" id="authorizeButton" style="display: none" value="Authorize" />
<p id="notice" style="display: none">check browser console for output</p>
</body>
</html>

关于javascript - 检索最后 5 条未读消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25297607/

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