0

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?

2
  • 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. Commented Mar 7, 2017 at 14:56
  • 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. Commented Mar 7, 2017 at 17:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.