Search code examples
javascriptnode.jsgmailgmail-api

GmailAPI - Node.js - Cannot update user signatures


I have authorised a small node app with the gmail API. I am successfully getting responses. I can, for eg, see my email signature using users.settings.sendAs.list()

However When I try and make a change to the settings, again in this case the signature, the response I get back is empty. So the update is working in so far as i can wipe a signature I added manually in gmail, but I cannot add anything new.

Here's the script

var google = require('googleapis');
var key = require('./Sig-Updater.json');
var gmail = google.gmail('v1');

var jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/gmail.settings.basic', 'https://www.googleapis.com/auth/gmail.settings.sharing', 'https://www.googleapis.com/auth/gmail.modify', 'https://mail.google.com', 'https://www.googleapis.com/auth/gmail.compose',
'https://www.googleapis.com/auth/gmail.readonly' ],
  '[email protected]'
);

jwtClient.authorize(function (err, tokens) {
  if (err) {
    console.log('Auth failed because: ' + err);
    return;
  }
  console.log(tokens)

  gmail.users.settings.sendAs.update({
    userId: 'me',
    auth: jwtClient,
    sendAsEmail: '[email protected]',
    signature: '<div dir="ltr">Hello there</div>'
  }, function (err, resp) {
    if(err){
      console.log(err);
    } else {
      console.log(resp);
    }
  });
});

Sorry for any noob business - new to this API and indeed node.js


Solution

  • Looks like my Noob disclaimer was warranted in the end,

    I had omitted the 'resource' object from the request.

    Should look more like

    gmail.users.settings.sendAs.update({
    userId: 'me',
    auth: jwtClient,
    sendAsEmail: '[email protected]',
    fields: 'signature',
    resource: {
        signature: '<div dir="ltr">Hello there</div>'  
        }    
    }, function (err, resp) {
        if(err){
          console.log(err);
        } else {
          console.log(resp);
        }
    });
    

    Hope that helps someone in the future!