0
votes

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?

1
Have you tried getting a list first developers.google.com/gmail/api/v1/reference/users/settings/… making your change to that then patching it back? You might also want to try using create instead of patch since you are actually creating a new one and trying to insert it.DaImTo
Hi DalmTo, thanks for helping. I think the issue is how app scripts decides what auth scopes are required. I've written near identical scripts that require different permissions. It's bizarre. I need a way of manually requesting an additional auth scope, perhaps.h0dges

1 Answers

1
votes

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.