1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67

1 Answers1

3

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}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • `e.pathInfo` is the thing I was looking for. Thank you very much for your detailed answer. – Shihan Khan May 12 '20 at 04:21
  • Can't send a post-query with `pathInfo`. It's not working now. – contributorpw Feb 14 '23 at 09:53
  • @contributorpw About `Can't send a post-query with pathInfo. It's not working now.`, unfortunately, in the current stage, in order to use `pathInfo`, it is required to use the access token. As a simple test, please log in to your Google account and access `https://script.google.com/macros/s/###/exec/sample.txt` with your browser. By this, `"pathInfo":"sample.txt"` is included. If you want to use this with a script, please request it with the access token. – Tanaike Feb 14 '23 at 11:42
  • 1
    @contributorpw Now, I updated my answer. Please confirm it. – Tanaike Feb 14 '23 at 11:56
  • @contributorpw Thank you for replying. I could notice this from your comment. Thank you, too. – Tanaike Feb 14 '23 at 13:21
  • @Tanaike would you mind telling me how to create the access token? – Craig Lambie Mar 15 '23 at 19:52
  • 1
    @Craig Lambie About `@Tanaike would you mind telling me how to create the access token?`, are these threads useful? https://stackoverflow.com/search?q=google+get+access+token or https://stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+get+access+token – Tanaike Mar 16 '23 at 00:22
  • 1
    Thanks Tanaike. Yes helpful. I had the key pushed to a sheet cell so I could use it, appears to work :) – Craig Lambie Mar 19 '23 at 03:37