2
votes

I am looking for a simple way to check programmatically if user authorization is required to run a Google Apps Script. I am calling the script from a Chrome Extension using HTTP GET and would like to show the Google's authorization request page only when required (typically on user's first run on the extension).

Some details:

Currently I make a "GET" XMLHttpRequest using withCredentials=true.

The request url format is 'https://script.google.com/macros/s/<..ID..>/exec?<..my params..>' which executes the doGet(e) function inside the script.

When the GAS is authorized, my doGet() returns a JSON text (which my Chrome Extension parses).

When the GAS is not authorized, doGet() returns an HTML text starting with "Authorization needed ..." and xhr.status stays 200.

I can try parsing this HTML string to detect if authorization is required but that seems fragile so I am looking for a better way.

1
I assume you're publishing the gas to run as the user accesing the script. If so, what response you currently when you GET? should return an error in there. Make sure you use withCredentialsZig Mandel
I edited the question to clarify things.dux2

1 Answers

1
votes

The way you suggest in your newly edited question is the correct way, you parse that response. Yes its a bit fragile as it could return other errors as well (exceptions, over quota, timeout etc).