h0dges
h0dges

Reputation: 693

Set email signature using Google Apps Script with Gmail API

I'm trying to set the Gmail signature of the user executing the script (Execute the app as: "User accessing the web app"; Who has access to the app: "Anyone within my domain") using the following function:

function setSignature(signature) {  
  var newSig = Gmail.newSendAs();
  newSig.signature = signature;
  Gmail.Users.Settings.SendAs.patch(newSig, "me", Session.getActiveUser().getEmail());
}

where signature is some html. This function is called from a client-side script when a form is submitted:

google.script.run.withSuccessHandler(signatureSuccess).setSignature($("#signatureParent").html());

The user is served a web app using the HtmlService containing the form. The Gmail API has been enabled in both the Advanced Google Services window as well as the Google API Console.

My issue is that when the I try and execute the function I receive the following console error message:

enter image description here

The message states that the auth scope gmail.settings.basic is missing. This is despite the user authorizing the web app before any html is served:

enter image description here

How do I fix or work around this issue?? The strange thing is I've had this working previously so I don't know what I'm doing wrong.

EDIT:

I've noticed that if I create a simple Apps Script with just the function:

function testSet() {  

  var testSig = "signature";
  var newSig = Gmail.newSendAs();
  newSig.signature = testSig;
  Gmail.Users.Settings.SendAs.patch(newSig, "me", Session.getActiveUser().getEmail());
}

And leave out everything else I get presented with these permissions to authorize:

enter image description here

If I click Allow it works! So clearly "Manage your basic mail settings" a.k.a. auth scope gmail.settings.basic is required and isn't being asked for in the more involved script.

So how do I force that permission to be acquired or how do I rewrite my script to get the correct set of permissions needed?

Upvotes: 0

Views: 4002

Answers (2)

Bhavnesh Sharma
Bhavnesh Sharma

Reputation: 1

I was also facing the same issue on nodejs application, the solution is to generate referesh token using this required scope which is mentioned in the rest api documentation find below. rest apis documentation

you can create refresh token using required scopes on this link if you're logged in developer account. https://developers.google.com/oauthplayground:

Upvotes: 0

h0dges
h0dges

Reputation: 693

After extensive testing I've determined that this issue is a bug in Google Apps Script in determining what scopes are required.

A basic version of my script requires these scopes (File > Project Properties > Scopes):

enter image description here

Extending the script to interact with Google Drive modifies the scopes to this:

enter image description here

By dropping the required gmail.settings.basic scope a critical function within the script is denied permission to run. Infuriating.

Upvotes: 1

Related Questions