Grumpledorf
Grumpledorf

Reputation: 11

Google Script not asking for Auth: ScriptError: Authorization is required to perform that action

So I'm following the examples on here: https://developers.google.com/apps-script/guides/html/reference/run and they don't work. I get the error not in the return from the failure handler. Google never asked for auths. I figured out how to add auths manually to the manifest but idk what to add to fix it.

MainScript.gs

function doGet() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(HtmlService.createHtmlOutputFromFile('Index'), 'test');
}

function getUnreadEmails() {
  // 'got' instead of 'get' will throw an error.
  Logger.log("yes");
  return GmailApp.getInboxUnreadCount();
}

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function onFailure(error) {
        var div = document.getElementById('output');
        div.innerHTML = error;
      }

      google.script.run.withFailureHandler(onFailure)
          .getUnreadEmails();
    </script>
  </head>
  <body>
    <div id="output"></div>
    <button onclick="google.script.host.close()">Cancel</button>
  </body>
</html>

Resulting Dialogue Box: enter image description here

Upvotes: 1

Views: 249

Answers (1)

Cooper
Cooper

Reputation: 64100

This worked for me:

Just run showMyTestDialog();

I think the difference was that you need to add the withSuccessHandler(). Most of the time I don't use the failure handler.

html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
       google.script.run
      .withFailureHandler((msg)=>{document.getElementById('output').innerHTML=msg})
      .withSuccessHandler((msg)=>{document.getElementById('output').innerHTML=msg})
      .getUnreadEmails();
    </script>
  </head>
  <body>
    <div id="output"></div>
    <button onclick="google.script.host.close()">Cancel</button>
  </body>
</html>

gs:

function getUnreadEmails() {
  return "Hello World"
}

function showMyTestDialog() {
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile('ah1'), 'test');
}

Some of it's written slightly different but it's the same thing really. Feel free to ask questions. I tend to be rather light in the explanation department.

Upvotes: 1

Related Questions