Skip to content

Instantly share code, notes, and snippets.

@theirf
Created September 9, 2014 22:05
Show Gist options
  • Save theirf/f142d3b02e5e2343403a to your computer and use it in GitHub Desktop.
Save theirf/f142d3b02e5e2343403a to your computer and use it in GitHub Desktop.
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<script type="text/javascript">
var CLIENT_ID = '*************************************.apps.googleusercontent.com';
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.
*/
var threadID;
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) {
for(var i = 0; i<5; i++){
getThreads(USER, resp["threads"][i]["id"], function(response){
console.log(response);
});
getThreads("me", "THREAD_ID", function (dataMessage) {
var temp = dataMessage.messages[0].payload.headers;
$.each(temp, function (j, dataItem) {
if (dataItem.name == "From") {
console.log(dataItem.value);
}
});
});
}
});
});
} 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);
}
function getThreads(userId, id){
var request = gapi.client.gmail.users.threads.get({
'userId':userId,
'id':id
});
}
</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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment