admin管理员组文章数量:1394213
In the code below I am signing in, authorising the app, and getting console output via the GMail API. I believe I am getting the threads and thread IDs, but I am not seeing the messages in the console.
I am not getting any errors and I am getting output, just what seems like keys with no values.
Here is what the console output looks like:
Here is the code:
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['.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 plete.
*/
function listThreads(userId, callback) {
var request = gapi.client.gmail.users.threads.list({
'userId': userId
});
request.execute(callback);
}
How can I retrieve the from address, subject, and body of the messages? with the GMAIL API in js
**Update: What I am currently working with: **
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);
}
});
});
});
});
When I log dataMessage, I get a 400 error, ' id required '. When I log dataItem.value, I get a dataMessage.messages is undefined and can not have an index of 0.
I'd greatly appreciate help in getting this working!
In the code below I am signing in, authorising the app, and getting console output via the GMail API. I believe I am getting the threads and thread IDs, but I am not seeing the messages in the console.
I am not getting any errors and I am getting output, just what seems like keys with no values.
Here is what the console output looks like:
Here is the code:
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['https://www.googleapis./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 plete.
*/
function listThreads(userId, callback) {
var request = gapi.client.gmail.users.threads.list({
'userId': userId
});
request.execute(callback);
}
How can I retrieve the from address, subject, and body of the messages? with the GMAIL API in js
**Update: What I am currently working with: **
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);
}
});
});
});
});
When I log dataMessage, I get a 400 error, ' id required '. When I log dataItem.value, I get a dataMessage.messages is undefined and can not have an index of 0.
I'd greatly appreciate help in getting this working!
Share Improve this question edited Sep 16, 2014 at 2:27 wordSmith asked Aug 17, 2014 at 20:21 wordSmithwordSmith 3,1839 gold badges33 silver badges52 bronze badges3 Answers
Reset to default 1GMail api in Javascript does not explicit methods to access particular email part - to/from/etc. GMail api in Java has this feature. Gmail api in Javascript are still in Beta. api list
You still wanna do it: Here is outline:
Instead of getting list of threads get list of messages: message list
Parse message id from json retrieved from previous call, use it with following: message get
Get raw message in URL encoded base64 format. Decode and parse. safe encoding encoding
Difficult... You bet... :)
Like Amit says above you can use messages.list() to get a list of message ids. With those you can simply call messages.get() and that will return an email in parsed form and you can get to the headers via message.payload.headers. You don't need to get the 'raw' message base64 encoded.
Here's what i did to get the from email id from message
After calling the listThread()
method, i called getThread()
method to fetch the from email ID from that thread as following.
listThreads("me", "", function (dataResult) {
$.each(dataResult, function (i, item) {
getThread("me", item.id, function (dataMessage) {
var temp = dataMessage.messages[0].payload.headers;
$.each(temp, function (j, dataItem) {
if (dataItem.name == "From") {
Console.log(dataItem.value);
}
});
});
});
});
Similarly you can fetch other details from the message.
Reference : JSON Format for the message
本文标签: javascriptGet messages in users39 GMail inboxStack Overflow
版权声明:本文标题:javascript - Get messages in users' GMail inbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744746914a2622937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论