google-drive-apigoogle-docs-apistatic-pages

Reading files on google drive without any auth


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?


Solution

  • 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