4
votes

I'm trying to use server side node.js to send a request to the Gmail Send Message API without success. I'm getting the following error:

 body: '{
 "error": {
  "errors": [
   {
    "domain": "global", 
    "reason": "invalidArgument",
    "message": "\'raw\' RFC822 payload
     message string or uploading message via /upload/ URL required"  
   }
  ],
  "code": 400,
  "message": "\'raw\' RFC822 payload message
   string or uploading message via /upload/ URL required"
 }
}'
}

The input parameters of oauth2token and raw are valid, in-fact if I use Google OAuth 2 playground(https://developers.google.com/oauthplayground) and use the token and raw as values the email DOES send. Can someone see what I've missed?

    function sendMail(oauth2token, raw) {
    context.log('Token: ' + oauth2token);
    context.log('raw: ' + raw);

    var params = {
        userId: user_id,
        resource: { 'raw': raw}
    };

    var headers = {
        "HTTP-Version": "HTTP/1.1",
        "Content-Type": "application/json",
        "Authorization": "Bearer " + oauth2token
    };

    var options = {
        headers: headers,
        url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
        method: "POST",
        params: params
    };

    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            context.log(body);
        }
        if (error) {
            context.log(error);
        }
        else {
            context.log(response);
        }
    })
}
2
I have the same issue. Did you ever solve this?grabbag

2 Answers

0
votes

If you are testing in the Google playground and all looks good, I'd start looking at other external dependencies that you are using. For example, request. Maybe you need to pass in a parsed url object instead of the url. Check this out: https://github.com/request/request#requestoptions-callback.

Here is what your parsed url object would look like:

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.googleapis.com',
  port: null,
  hostname: 'www.googleapis.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/gmail/v1/users/me/messages/send',
  path: '/gmail/v1/users/me/messages/send',
  href: 'https://www.googleapis.com/gmail/v1/users/me/messages/send' }

Either that or change your existing options to uri instead of url

0
votes

You only need to pass the raw message in the body:

function sendMail (oauth2token, raw) {
  var options = {
    method: 'POST',
    url: 'https://www.googleapis.com/gmail/v1/users/me/messages/send',
    headers: {
      'HTTP-Version': 'HTTP/1.1',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + oauth2token
    },
    body: JSON.stringify({
      raw: raw
    })
  };

  request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      context.log(body);
    }
    if (error) {
      context.log(error);
    } else {
      context.log(response);
    }
  });
}