Gmail的API - 解析消息内容(Base64编码解码?)使用Javascript

问题描述:

我想使用Gmail的API来获取用户的电子邮件,抢在邮件主题和正文,然后在网页上显示。我会做其他的东西吧,但是这是我有困难的部分。我使用Angular.js。

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.

下面是我的API调用:

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

所以 gapi.client.gmail.users.messages.list 返回我的消息的数组,用自己的ID号。这是工作。

So gapi.client.gmail.users.messages.list returns an array of my messages, with their ID numbers. That is working.

gapi.client.gmail.users.messages.get调用({&lt;具体消息ID&GT;})输出这一点 - {\"B\":{\"method\":\"gmail.users.messages.get\",\"rpcParams\":{},\"transport\":{\"name\":\"googleapis\"}}}$c$c>.

不知道那是什么,而是试图得到的消息有效载荷( email.payload.parts ),结果未定义。所以,我怎样才能得到消息的内容?

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?

另外,我会假设,如果我能得到的消息内容,我将不得不为Base64德code的内容得到一些英语出来。对于任何建议将是很大的帮助也。我发现这一点: https://github.com/kvz/phpjs ,但怎么走,因为我不知道有关获取消息内容,这样我可以尝试去code他们,所以不知道这是php.js在这方面的帮助。

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.

根据您的电子邮件是什么样子(单text / plain的一部分吗?用文本/ HTML多部分?附件等?),你可能会或可能不会有任何部分在您的email.payload,而是你有你在找什么在email.payload.body.data(单部分消息)。这一切都是假设你正在做的默认格式(全)一message.get。如果你不是想在message.raw领域的整个电子邮件和电子邮件库处理它为你的语言,你可以调用message.get(格式=原)。

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).

有关更多信息请查看身体和份[]字段文档消息在的 https://developers.google.com/gmail/api/v1/reference/users/messages

For more info check out the "body" and "parts[]" field documentation for "Message" at https://developers.google.com/gmail/api/v1/reference/users/messages