0

I'm new to Web Scripts and deploying web apps, I have an issue where I require users to authorize permission to use the web app, but nothing pops up to request for said authorization.

I found this post here and it seems to be what I needed but I'm unsure how would I use it. I tried calling onOpen function at the start of my doGet but nothing happens. I also don't see where/when the authorize function is being called. My Web App is also using sheets, so I deleted any lines of code dealing with DocumentApp.

Here is my modified version of the code

function onOpen() {
  if(!UserProperties.getProperty('author')){
    var html = HtmlService.createHtmlOutputFromFile('index1')
    .setTitle("Install Menu").setWidth(400);
  }else{
    var html = HtmlService.createHtmlOutputFromFile('index2')
    .setTitle("Mailmerge Menu").setWidth(400);
  }
}

function authorize(){
  UserProperties.setProperty('author','yes');
  var html = HtmlService.createHtmlOutput('Authorization complete<br>Thanks<br><br>please refresh your browser').setTitle("Confirmation").setWidth(400);
}

This is my doGet function just in case I was not utilizing the functions correctly.

function doGet(e)
{
  onOpen();

  var result;
  
  var sheets = SpreadsheetApp.getActiveSheet();
  var values = [];

  // Get row 1 data
  for( var i = 3; i <= 16; ++i)
  {    
    var char = String.fromCharCode('A'.charCodeAt() + i) // Increment char
    values.push(sheets.getRange(char + 1).getValue());
  }

  // Get row 2 data
  for( var i = 3; i <= 16; ++i)
  {    
    var char = String.fromCharCode('A'.charCodeAt() + i) // Increment char
    values.push(sheets.getRange(char + 2).getValue());
  }
  
  var result = {result:values};


  var JsonValue = JSON.stringify(result);
  
  return ContentService.createTextOutput(JsonValue.toString()).setMimeType(ContentService.MimeType.JSON);
}

I know I can set the web app to execute as me by anyone, which does work, but I would like be able to allow other users to authorize themselves to execute the app.

Edit: I'm using Unity's WebRequest class to call the Get from my web app.

_webRequest = UnityWebRequest.Get("web app url here");
_webRequest.SendWebRequest();

And on receiving the response I will parse it and use the data in "result" in a separate class

if (_webRequest != null && _webRequest.isDone)
{
    result = JsonUtility.FromJson<WebResponsePacket> 
    (_webRequest.downloadHandler.text);
     Debug.Log("Successful Request");

    return true;
}
DerpDerp
  • 1
  • 3
  • 1
    A Web App can only be published to either execute as you or execute as the user. It can't do both. Of course, if you publish it to execute as the user, then you can log in as the user even though you're the owner. Who owns the Google Sheet? Is each user using their own Google Sheet, or is every user using your Google Sheet? – Alan Wells May 02 '21 at 18:42
  • @DerpDerp If the add-on execution settings are set to execute as the user the log-in prompt will automatically appear. Make sure your settings are correct and that you are not logged in. Also remember the web app URL changes on each deployment (if using the new editor) – Rafa Guillermo May 03 '21 at 08:02
  • @RafaGuillermo How do I check if I'm logged in (on mobile). The web app URL did not change when I redeploy with the executing user settings even though I'm using the new editor. – DerpDerp May 03 '21 at 10:16
  • @AlanWells I own the google sheet, the app is simply to allow users to upload/download data from the sheet directly through a mobile app. I would like to publish it to execute as user, but I'm unable get a log in prompt to appear – DerpDerp May 03 '21 at 10:18
  • @RafaGuillermo I tried again with all google accounts signed out from the device but still nothing unfortunately. Could my deployment settings be wrong by chance? Currently I'm executing as user accessing the web app and anyone with google account can access it – DerpDerp May 03 '21 at 10:25
  • @DerpDerp visit google.com and see if you're logged in. It'll be the same across the platform. – Rafa Guillermo May 03 '21 at 11:55
  • @DerpDerp If your settings are 'user accessing the web app' then a user has to be signed in. Otherwise you need to use just 'Anyone'. – Rafa Guillermo May 03 '21 at 11:55
  • @RafaGuillermo I tried visiting google.com and I wasn't logged in, when using the app. I did not receive a prompt. Could the issue lie in my app? Do I need to have additional code to allow for the login prompt? All my app does is run Get and Post request. Additionally is there any reason, security or for example handing over the app to developer. that I shouldn't let the app execute as myself? – DerpDerp May 03 '21 at 12:25
  • @DerpDerp What *does* happen when you visit the web app when not logged in? – Rafa Guillermo May 04 '21 at 07:04
  • @RafaGuillermo My get request returns a html of the login request as a string – DerpDerp May 04 '21 at 10:48
  • Oh hang on, you're using fetch API or browser to access the web app? – Rafa Guillermo May 04 '21 at 13:38
  • @RafaGuillermo I think I'm using fetch API? I'm not quite too sure with the terminologies since this is my first time working with app scripts. I think my explanation was also kind of lacking, but I have a mobile app that sends Get and Post request from my web app. – DerpDerp May 04 '21 at 15:48
  • Can you provide the code you're using to fetch the web app? – Rafa Guillermo May 04 '21 at 15:59
  • @RafaGuillermo I edited my post with some additional code that could be useful for context – DerpDerp May 04 '21 at 16:31
  • Okay, please can you post screenshots of your deployment settings? – Rafa Guillermo May 05 '21 at 08:17
  • @RafaGuillermo This is my current deployment settings. https://imgur.com/a/HATxHOb – DerpDerp May 05 '21 at 09:04
  • Okay! You need to send an access token in the header of your web request in order to identify the user that's logged in. If you don't need a user to be logged in, then the "Who has access" field should be set to "Anyone", not "Anyone with a Google account". – Rafa Guillermo May 05 '21 at 09:55
  • @RafaGuillermo I'm sorry, I don't quite understand what do you mean. How do I get an access token, and do I need to get one for each user? How should sending an access token in the header look like and how would should I handle it in my app script? – DerpDerp May 05 '21 at 10:58
  • The access token will be provided through the OAuth2 process when the user authorises your application to act on their behalf. The issue is you will need to set up a consent screen for the user to log in, and way of forwarding the access token to your application. You can't just ping the WebApp as it's a programmatic way of getting the page and will never be shown to the user. – Rafa Guillermo May 05 '21 at 11:26
  • You'll have to use something like the [.NET client for google APIs](https://developers.google.com/api-client-library/dotnet) with the required application scopes so that the user will get a prompt and you can get an access token. You can then use that in the header of the HTTP request as `{'Authorization' : 'Bearer ' + accessToken}`. You don't need to handle it in your `doGet()` it just acts as a user identifier. – Rafa Guillermo May 05 '21 at 11:27
  • Check out the answer [here](https://stackoverflow.com/a/61654179/11551468) for more on OAuth2 for Google Workspace with C# which should give you a good place to understand the process with a snippet for using the client library – Rafa Guillermo May 05 '21 at 11:29
  • @RafaGuillermo Thank you very much for you help! I will take a look into this – DerpDerp May 05 '21 at 11:43

0 Answers0