简体   繁体   中英

Missing draft message - javascript Gmail API - how to structure body of the request?

At the recent Google IO conference new Gmail APIs were announced. Client libraries are missing examples and documentation which is understandable given the short time that has gone by.

UPDATE: It wasn't clear in the original question - I've already tried encoding the whole message as Base64 string.

I'm trying to create a new draft message:

var request = gapi.client.gmail.users.drafts.create({
    'message' : {
      'raw' :  Base64.encode("To: someguy@example.com\r\nFrom: myself@example.com\r\nSubject: my subject\r\n\r\nBody goes here")
      // 'raw' : "VG86IHNvbWVndXlAZXhhbXBsZS5jb20KRnJvbTogbXlzZWxmQGV4YW1wbGUuY29tClN1YmplY3Q6IG15IHN1YmplY3QKCkJvZHkgZ29lcyBoZXJl"
      // 'raw' : "From: me@example.com\nTo:you@example.com\nSubject:Ignore\n\nTest message\n"

    }
});

request.execute(function(response) {

});

Can you please provide me with the correct syntax to do that?

(Base64.encode is coming from http://www.webtoolkit.info/javascript-base64.html - tried using plain text, encoded version on the fly and hardcoded values from other question)


Related questions:

Handy links just for reference:


So I'm trying to find a solution in related questions addressing Ruby and C# by recreating JSON structure but I've reached the point that I need a Rubber Duck or Stack Overflow.

Thank you in advance for providing a hint on how to structure the object passed to the API method.

Since the question is the same, the answer will be identical:

'raw' should contain the entire (RFC822) email, complete with body and headers.

While @rds answer is technically correct: "base64 encode complete message", the fully working answer is as follows... The correct structure of the request:

'draft': {
  'message': {
    'raw': base64EncodedEmail
  }
}

Source: https://developers.google.com/gmail/api/v1/reference/users/drafts/create (scroll down and then choose JavaScript from dropdown menu)

I was missing the essential draft property.

The trick is it's not just normal base64 encoding it's WEB SAFE (aka URL SAFE) base64 encoding. It's similar except two characters in the alphabet are different to make sure the entire blob works well in URLs and javascript/json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM