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