Search code examples
javascriptgoogle-apps-scriptoauth-2.0google-oauth

Apps Scripts OAuth2 authorization using gapi from inside HtmlService


I'm trying to be OAuth2-authorized by using the gapi library from inside an HtmlService project. I've got my CLIENT_ID from the console and I've set up the "Authorized Javascript Origins" with all of what I could think about like "https://script.google.com", "https://script.googleusercontent.com", etc, but I always end with an "origin mismatch" error.

This is the code I'm using from inside the html javascript code.

function checkAuth() {
  gapi.auth.authorize({
    'client_id': CLIENT_ID,
    'scope': SCOPES,
    'immediate': false
  }, handleAuthResult);
}

What am I doing wrong?

Thanks


Solution

  • To get the origin you need launch your webapp. Right click and click Inspect. This should open Devtools. Click on the Elements tab. Look for the iframe with the id="sandboxFrame". The source the that iframe in your webapp origin. It will look like:

    webapp origin as seeen in devtools

    When you add it to your credentials just add the domain part leaving off the endpoint.

    https://n-rilyfaex6q3..iimkn7ufgvlphhi2..l3l73y-0lu-script.googleusercontent.com