Search code examples
google-apps-scriptweb-applications

Use path/slug after Web App's base url in Google Apps Script


I'm looking to make the url by adding a path which is something like this below in Google Apps Script:

https://script.google.com/macros/s/APP_ID/exec/fileName.txt

How can I achieve this for Web App service?


Solution

  • I believe your goal as follows.

    • You want to access to Web Apps using the URL of https://script.google.com/macros/s/APP_ID/exec/fileName.txt.

    For this, how about this answer? I think that you can achieve your goal using Web Apps. As a sample case, I would like to explain about this using a sample script for downloading a text file, when an user accesses to https://script.google.com/macros/s/APP_ID/exec/fileName.txt.

    Usage:

    Please do the following flow.

    1. Create new project of Google Apps Script.

    Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.

    If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.

    2. Prepare script.

    Please copy and paste the following script (Google Apps Script) to the script editor. This script is for the Web Apps.

    function doGet(e) {
      const path = e.pathInfo;
      if (path == "filename.txt") {
        const sampleTextData = "sample";
        return ContentService.createTextOutput(sampleTextData).downloadAsFile(path);
      }
      return ContentService.createTextOutput("Wrong path.");
    }
    
    • In order to retrieve the value of fileName.txt in https://script.google.com/macros/s/APP_ID/exec/fileName.txt, please use pathInfo.
      • For example, when you check e of doGet(e) by accessing with https://script.google.com/macros/s/APP_ID/exec/fileName.txt, you can retrieve {"contextPath":"","contentLength":-1,"parameter":{},"parameters":{},"queryString":"","pathInfo":"fileName.txt"}.
    • In this case, the GET method is used.

    3. Deploy Web Apps.

    1. On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
    2. Select "Me" for "Execute the app as:".
      • By this, the script is run as the owner.
    3. Select "Anyone, even anonymous" for "Who has access to the app:".
      • In this case, no access token is required to be request. I think that I recommend this setting for your goal.
      • Of course, you can also use the access token. At that time, please set this to "Anyone". And please include the scope of https://www.googleapis.com/auth/drive.readonly and https://www.googleapis.com/auth/drive to the access token. These scopes are required to access to Web Apps.
    4. Click "Deploy" button as new "Project version".
    5. Automatically open a dialog box of "Authorization required".
      1. Click "Review Permissions".
      2. Select own account.
      3. Click "Advanced" at "This app isn't verified".
      4. Click "Go to ### project name ###(unsafe)"
      5. Click "Allow" button.
    6. Click "OK".
    7. Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
      • When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.

    4. Run the function using Web Apps.

    Please access to https://script.google.com/macros/s/###/exec/filename.txt using your browser. By this, a text file is downloaded.

    Note:

    • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.

    References:

    Updated on February 14, 2023

    In the current stage, it seems that pathInfo can be used with the access token. It supposes that the following sample script is used.

    function doGet(e) {
      return ContentService.createTextOutput(JSON.stringify(e));
    }
    

    When you log in to your Google account and you access https://script.google.com/macros/s/###/exec/sample.txt with your browser, {"contextPath":"","parameter":{},"pathInfo":"sample.txt","contentLength":-1,"parameters":{},"queryString":""} can be seen.

    In this case, when you access it without logging in Google account, even when Web Apps is deployed as Execute as: Me and Who has access to the app: Anyone, the log in screen is opened. Please be careful about this.

    And, if you want to access with https://script.google.com/macros/s/###/exec/sample.txt using a script, please request it by including the access token. The sample curl command is as follows. In this case, the access token can be used as the query parameter. Please include one of the scopes of Drive API in the access token.

    curl -L "https://script.google.com/macros/s/###/exec/sample.txt?access_token=###"
    

    By this, the following result is returned.

    {"contextPath":"","queryString":"access_token=###"},"pathInfo":"sample.txt","parameters":{"access_token":["###"]},"contentLength":-1}