4

I want to send an email through Google API without the unnecessary OAUTH2 parameters. I only have the access_token and the refresh_token of that user.

How can I send an email through Gmail API through a basic POST request in NodeJS, with Request npm plugin?

Yagiz
  • 1,033
  • 2
  • 21
  • 47

2 Answers2

7

abraham is correct, but I just thought I'd give you an example.

var request = require('request');

server.listen(3000, function () {
  console.log('%s listening at %s', server.name, server.url);

  // Base64-encode the mail and make it URL-safe 
  // (replace all "+" with "-" and all "/" with "_")
  var encodedMail = new Buffer(
        "Content-Type: text/plain; charset=\"UTF-8\"\n" +
        "MIME-Version: 1.0\n" +
        "Content-Transfer-Encoding: 7bit\n" +
        "to: reciever@gmail.com\n" +
        "from: sender@gmail.com\n" +
        "subject: Subject Text\n\n" +

        "The actual message text goes here"
  ).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');

  request({
      method: "POST",
      uri: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
      headers: {
        "Authorization": "Bearer 'access_token'",
        "Content-Type": "application/json"
      },
      body: JSON.stringify({
        "raw": encodedMail
      })
    },
    function(err, response, body) {
      if(err){
        console.log(err); // Failure
      } else {
        console.log(body); // Success!
      }
    });
});

Don't forget to change the reciever and sender email address for the example to work.

Tholle
  • 108,070
  • 19
  • 198
  • 189
  • 1
    This answer solved my issue with invalid credentials.Thanks a lot. – Shashidhar Gr Sep 11 '15 at 04:02
  • @ShashidharGr Awesome :) Glad I could help! – Tholle Sep 11 '15 at 09:39
  • If I use `https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send` I am receiving the error Media type 'application/json' is not supported. Valid media types: [message/rfc822]. I need to send an email with attachments – Tiger developer May 29 '17 at 18:14
4

There are two methods for attaching OAuth2 access_tokens to a Google API request.

  • Using the access_token query parameter like this: ?access_token=oauth2-token
  • Using the HTTP Authorization header like this: Authorization: Bearer oauth2-token

The second one is prefered for POST requests so the raw HTTP request for sending an email would look something like this.

POST /gmail/v1/users/me/messages/send HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer oauth2Token
{"raw":"encodedMessage"}
abraham
  • 46,583
  • 10
  • 100
  • 152
  • What is the difference between using Bearer or OAuth in Authorization header? Example: `'Authorization': 'OAuth ' + account.access_token, – Yagiz Apr 08 '15 at 16:44
  • `Authorization: Bearer ...` is [OAuth 2](https://tools.ietf.org/html/rfc6749#section-7.1), `Authorization: OAauth ...` is [OAuth 1](https://tools.ietf.org/html/rfc5849#section-3.5.1). Although I'm sure there are other uses for both. – abraham Apr 08 '15 at 21:07