gpt4 book ai didi

javascript - 在用户的 GMail 收件箱中获取消息

转载 作者:搜寻专家 更新时间:2023-11-01 04:25:06 24 4
gpt4 key购买 nike

在下面的代码中,我将登录、授权应用程序并通过 GMail API 获取控制台输出。我相信我正在获取线程和线程 ID,但我没有在控制台中看到消息。

我没有收到任何错误,我得到了输出,只是看起来像没有值的键。

这是控制台输出的样子:enter image description here

代码如下:

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);
}

如何检索邮件的发件人地址、主题和正文?在 js 中使用 GMAIL API

**更新:我目前在做什么:**

listThreads('me', function(dataResult){
$.each(dataResult, function(i, item){
getThread('me', item.id, function(dataMessage){
console.log(dataMessage);
var temp = dataMessage.messages[0].payload.headers;
$.each(temp, function(j, dataItem){
if(dataItem.name == 'From'){
console.log(dataItem.value);
}
});
});
});
});

当我记录 dataMessage 时,我收到 400 错误,“需要 id”。当我记录 dataItem.value 时,我得到一个 dataMessage.messages 未定义并且不能有 0 的索引。

如果能帮助我完成这项工作,我将不胜感激!

最佳答案

Javascript 中的 GMail api 没有明确的方法来访问特定的电子邮件部分 - 收件人/发件人/等等。 Java 中的 GMail api 具有此功能。Javascript 中的 Gmail api 仍处于测试阶段。 api list

你还想做:这是大纲:

不是获取线程列表而是获取消息列表: message list

从上一次调用中检索到的 json 中解析消息 ID,将其与以下内容一起使用: message get

获取 URL 编码的 base64 格式的原始消息。解码和解析。 safe encoding encoding

困难...你打赌...:)

关于javascript - 在用户的 GMail 收件箱中获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25353304/

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