Google Drive File IDs to Personal Website using Drive API & Javascript

Viewed 27

I have a Google Drive folder filled with many audio files, and I want my website to play one at random when a user clicks a button. I have the logic setup already, and it's working if I manually enter the google drive links into my array like this:

var fileArray = ["https://docs.google.com/uc?export=open&id=XXXXXXX", "https://docs.google.com/uc?export=open&id=XXXXXXXXX"];              

The problem is I have hundreds of files, and I update the Drive often so I want it to update by itself. Thus where the Google Drive API comes in. Unfortunately, I have now found myself going from 1 line of code to many lines of code and being totally lost. I have an API_KEY & CLIENT_ID. The documentation at Drive API -> Javascript Quickstart has a "handleAuthClick" function, but I don't want the user to have to sign in as they will never be making changes to the Drive. I don't even see anywhere it enters the folder ID which I don't understand. People talking about javascript origins, redirect uris, google picker api. The documentation at Drive API -> files.get has cURL & http files, and I've also seen people talking about json files storing information like service account details. Is it possible to just keep everything in my javascript script? Here is an example of some code I've tried:

<script src="https://apis.google.com/js/client:platform.js"></script>
<script type="text/javascript">
   var fileArray = [];
   const API_KEY = 'XXXXX';
   const CLIENT_ID = 'XXXXX';
   const CLIENT_SECRET = 'XXXXXX';
   const REDIRECT_URI = 'https://developers.google.com/oauthplayground';
   const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest';
   const { google } = require('googleapis');
   const SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly';
   function getFileIDs() {
       var folderId = 'XXXXXX';
       let response;
       try {
           response = await gapi.client.drive.files.list({
           'fileId': folderId,
           'fields': 'files(id)',
           });
           for (var i = 0; i < files.length; i++){
               var currentID = files[i].id;
               var currentDriveLink = 'https://docs.google.com/uc?export=open&id=' + currentID;
               fileArray.push(currentDriveLink);
           }
       }
   }
</script>         

I just want the minimum code to get to file.id, and then I can prepend the URL structure and push it to my array. Any thoughts are helpful thank you.

0 Answers
Related