0

Is it possible to read a file from the drive thats open to public via api without the need of any authentication?

I have a static application built exclusively in javascript without any backend and Im planning to write a json in a file on google docs or drive, then read the json via api, but I cant use any thing that involves password because of the fact that people could just inspect the code and get the credentials. Can this be done with the drive/docs api or any other else where I need a password to write the file but not to read via request?

  • In your situation, you want to retrieve the file contents of a text file on your Google Drive without authorizing, when the contents are retrieved. Is my understanding correct? In this situation, the file is publicly shared? – Tanaike Mar 21 '21 at 03:30
  • Yes, the file will be public shared, but i want to give the file to a friend so he can edit the json adding keys to it and it should reflect in the page without him being forced to actually edit the json and commit on git (wich may be hard for ppl who don't code, even if i give him the commands for just copy and paste) – Máttheus Spoo Mar 22 '21 at 12:41
  • Thank you for replying. I have to apologize for my poor English skill. Unfortunately, from your replying, I cannot still understand about your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I deeply apologize I cannot resolve your issue soon. – Tanaike Mar 22 '21 at 12:54
  • Is the file shared with "Anyone with the link?" – Aerials Mar 22 '21 at 13:33
  • i don't mind sharing the file in any way. – Máttheus Spoo Mar 22 '21 at 18:24
  • @Tanaike i have a application that consumes a json, and i want that json to be stored on google drive so a friend who doesn't know how to code be able to change small things on it without having to commit stuff. The problem is that my app doesn't have a back end, so i can't authenticate to retrieve the file, even if it's shared, since doing so would expose my credentials on the code. – Máttheus Spoo Mar 22 '21 at 18:27
  • @Aerials yeah, it's shared with 'anyone with the link' – Máttheus Spoo Mar 22 '21 at 18:27
  • @MáttheusSpoo how did this go for you? Did you try the web app approach? – iansedano Apr 01 '21 at 07:12
  • @iansedano no, since i couln't find a awnser in time, i gave up on the drive idea and made my friend create a account on github and edit a static json there where i can consume with a simple get. – Máttheus Spoo Apr 05 '21 at 13:15

1 Answers1

1

You can serve it with an Apps Script Web app

This will let you have a very light-weight and easy-to-manage back-end.

Create a new script and then paste this code for example:

function doGet() {

  // from https://json.org/example.html
  let json = {
    glossary: {
      title: "example glossary",
      GlossDiv: {
        title: "S",
        GlossList: {
          GlossEntry: {
            ID: "SGML",
            SortAs: "SGML",
            GlossTerm: "Standard Generalized Markup Language",
            Acronym: "SGML",
            Abbrev: "ISO 8879:1986",
            GlossDef: {
              para:
                "A meta-markup language, used to create markup languages such as DocBook.",
              GlossSeeAlso: ["GML", "XML"],
            },
            GlossSee: "markup"
          }
        }
      }
    }
  };

  return ContentService
    .createTextOutput(JSON.stringify(json))
    .setMimeType(ContentService.MimeType.JSON);
    
}

Replace the json variable, with the json content that you want.

Then deploy the web app:

enter image description here

Making sure that you select web app, and:

enter image description here

After which you will get a URL in this format:

https://script.google.com/macros/s/[ID]/exec

So now you can make a GET request to this endpoint to get the JSON! For example, if you visit it in the browser, you get:

enter image description here

References

iansedano
  • 6,169
  • 2
  • 12
  • 24
  • this looks promising, are you sure that the source code from the web app is protected and not visible to anyone but the person that deployed, tho? I may re-do what i did to use that in the future. – Máttheus Spoo Apr 05 '21 at 13:33
  • Yup, if you don't share it with anyone, it will only be visible to you. By share I mean in the editor/source code, anyone will be able to call the endpoint. – iansedano Apr 05 '21 at 13:35