Gmail API - Parse message content (Base64 decoding?) with Javascript
angularjs, base64, gmail-api, javascript
Solution
Depending on what your emails look like (single text/plain part? multipart with text/html? attachments, etc?) you may or may not have any "parts" in your email.payload and instead you'll have what you're looking for in "email.payload.body.data" (for single-part messages). This is all assuming you're doing a message.get with the default format ("full"). If you instead want to get the entire email in the message.raw field and deal with it in email libraries for your language you can call message.get(format=raw).
For more info check out the "body" and "parts[]" field documentation for "Message" at https://developers.google.com/gmail/api/v1/reference/users/messages
Problem
I'm trying to use the Gmail API to get a user's email, grab the message subject and body, and then display it on a webpage. I'll be doing other stuff with it, but this is the part that I am having difficulty with. I am using Angular.js. Here is my API call: ``` function makeApiCall() { gapi.client.load('gmail', 'v1', function() { var request = gapi.client.gmail.users.messages.list({ labelIds: ['INBOX'] }); request.execute(function(resp) { var content = document.getElementById("message-list"); angular.forEach(resp, function(message) { var email = gapi.client.gmail.users.messages.get({'id': message.id}); // var raw = email.payload.parts; // console.log(raw); content.innerHTML += JSON.stringify(email) + "<br>"; }) }); }); } ``` So `gapi.client.gmail.users.messages.list` returns an array of my messages, with their ID numbers. That is working. The call to `gapi.client.gmail.users.messages.get({<specific message ID>})` outputs this - `{"B":{"method":"gmail.users.messages.get","rpcParams":{},"transport":{"name":"googleapis"}}}`. Not sure what that is, but trying to get the message payload (`email.payload.parts`), results in `undefined`. So, how can I get the message content? Also, I would assume that if I can get the message contents, I would then have to Base64 decode the contents to get some English out of it. Any suggestions for that would be of great help also. I've found this: https://github.com/kvz/phpjs, but since I'm not sure how to go about getting the message contents so that I can try and decode them, so not sure if that php.js is of an help in that regard.