` function gMailInserMessage (sender_name, sender_email, msg_text) {
// Create a new JWT client using the key file downloaded from the Google Developer Console
const jwtClient = new google.auth.JWT(
postoffice_key.client_email,
null,
postoffice_key.private_key,
_.values(config_key.scopes.postoffice),
config_key.admin_email // subject (or sub) impersonated user
);
return jwtClient.authorize().then(tokens => {
// Obtain a new gmail client, making sure we pass along the auth client
const gmail = google.gmail({
version: 'v1',
auth: jwtClient
});
// Base64-encode the mail and make it URL-safe
// (replace all "+" with "-" and all "/" with "_")
var encodedMessage = btoa([
'From: ' + sender_email + '\r\n',
'To: ' + config_key.admin_email + '\r\n',
'Subject: Contact\r\n\r\n',
msg_text
].join("")).replace(/\+/g, '-').replace(/\//g, '_');
//Make an authorized request to insert a User Messages
return gmail.users.messages.insert({
userId: config_key.admin_email,
internalDateSource: 'receivedTime',
neverMarkSpam: true,
resource: {
raw: encodedMessage
}
});
}).then(res => res);
}`
postoffice_key.client_email being the service account admin email address, this service accound has Domain-Wide Delegation Authority set ( users.message.send() is running fine...)
I am trying to execute the users.messages.insert() request as specified in the GMail API v1 (https://developers.google.com/gmail/api/v1/reference/users/messages/insert) but I get an error: could not handle the request...
Here is my script
postoffice_key.client_email being the service account admin email address, this service accound has Domain-Wide Delegation Authority set ( users.message.send() is running fine...)