14

Is there a way to get a file listing without using authorization for a publicly hosted Goolge Drive folder?

Example folder: https://googledrive.com/host/0B1Nmfb7VDM6jNnhjWk9CdU9ueHc/

I want to use the folder to host images that I will display in a gallery on other sites.

Art
  • 1,519
  • 2
  • 13
  • 21
  • I am closing this question because Google has discontinued drive hosting. http://googleappsupdates.blogspot.com/2015/08/deprecating-web-hosting-support-in.html https://support.google.com/drive/answer/2881970?hl=en – Art Oct 22 '15 at 15:50
  • 4
    but its still relevant now, even without the hosting. – B''H Bi'ezras -- Boruch Hashem Apr 21 '20 at 11:55

7 Answers7

25

OK there is a way and it's pretty simple, it just needs a bit of setup. We need to get a Google Drive URL that can return JSONP, then run the request against it.

The setup

Generate the public URL. Go to: https://developers.google.com/apis-explorer/?hl=en_GB#p/drive/v3/drive.files.list

In the 'q' field enter the following:

'{your_public_folder_id}' in parents

Click the "Execute without OAuth" link underneath the button.

You should now see all your files listed underneath. It also show you the GET request. That is the URL you will need.

Copy the Request URL. Something like: https://www.googleapis.com/drive/v3/files?q='{your_public_folder_id}'+in+parents&key={YOUR_API_KEY}

Now, you will need to generate the API key. Go to Google Console: https://console.developers.google.com/ In left menu select 'Credentials'. Click 'Create credentials' and select API key. Click 'Browser key' Copy the generate key.

The execution

You can now do something like this:

var api_key = '{YOUR_API_KEY}';
var folderId = '{your_public_folder_id}';
var url = "https://www.googleapis.com/drive/v3/files?q='" + folderId + "'+in+parents&key=" + api_key;
var promise = $.getJSON( url, function( data, status){
    // on success
});
promise.done(function( data ){
    // do something with the data
}).fail(function(){

});
Mike Feltman
  • 5,160
  • 1
  • 17
  • 38
iDevGeek
  • 464
  • 5
  • 4
  • awesome!! super cool!! The only way/workaround to get the children of a folder without authentication I guess!! Kudos @iDevGeek :) – Srichakradhar Sep 02 '16 at 07:14
  • THis is great!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! And BTW for anyone else out there: once you have the API key, you can get a direct download link to any sized file in google drive with this link: https://www.googleapis.com/drive/v3/files/### fileId ###?alt=media&key=### API key ### see more: https://stackoverflow.com/questions/54588701/node-js-provide-direct-link-for-google-drive-file/54642410#54642410 – B''H Bi'ezras -- Boruch Hashem Feb 12 '19 at 04:22
  • could share a full link without your key? or example using api exploerer – Mahmoud Mabrok Sep 12 '20 at 05:39
3

There is a simple way to list a public folder - just use YQL to get XML or JSON results.

niutech
  • 28,923
  • 15
  • 96
  • 106
2

There's no way to do it unauthenticated. However, you can authenticate as ANY user and get the list with drive.children.list()

Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • How would you create the service instance on a server? – Games Brainiac Sep 28 '13 at 18:55
  • That's not the case. Maybe it was at the time this was posted but you can get the content of a public folder without having to authenticate. You will still need an API key. See my answer below please. – iDevGeek Jul 11 '16 at 03:51
2

One alternative solution --without using any API keys-- is to use google app-script to generate a text file for the directory, and put that script on a trigger every day or so to refresh it if the directory changes, then simply make that google doc file, that is modified by the google apps script, with the public URL plus /export?format=txt at the end, or for a regular file, something like https://drive.google.com/uc?export=download&id=INSERT_ID_HERE, and that is then publicly accessible on the web with no authorization

0

For those who still have a problem with this request, just use this template: https://www.googleapis.com/drive/v3/files?q=%27{YOUR FOLDER ID}%27+in+parents&key={YOUR API KEY} without the brackets. It really helps me, because these "'" characters problem is the drag

recvec
  • 894
  • 7
  • 12
0

Here is how you can do in PHP

  • Create a Project in your Google Cloud Console
  • Get a service account JSON file by going into Navigation Menu > IAM & Admin > Service Accounts
  • Run the following code to list all files in your requested Folder ID
    <?php

    require __DIR__ . '/vendor/autoload.php';

    if (php_sapi_name() != 'cli') {
        throw new Exception('This application must be run on the command line.');
    }

    $client = new Google_Client();
    // set the location manually
    $client->setAuthConfig('credentials.json');
    $client->setApplicationName("My Application");
    $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
    $service = new Google_Service_Drive($client);

    // Print the names and IDs for up to 100 files.
    $optParams = array(
        'pageSize' => 100,
        'fields' => 'nextPageToken, files(id, name)',
        'q' => "'YOUR_FOLDER_ID' in parents",
    );
    $results = $service->files->listFiles($optParams);

    if (count($results->getFiles()) == 0) {
        print "No files found.\n";
    } else {
        print "Files:\n";
        foreach ($results->getFiles() as $file) {
            printf("%s (%s)\n", $file->getName(), $file->getId());
        }
    }
Raja Amer Khan
  • 1,489
  • 10
  • 31
-1

Your expanded google drive url is this ,so use the expanded url as following to get all files ending in ".html" extension. This example uses ajax call using jquery.

Tweak the script according to your need.

<script type="text/javascript">
var dir = "https://d95b2b8c54c1827d7a316594f5436a81b5bb2b76.googledrive.com/host/0B1Nmfb7VDM6jNnhjWk9CdU9ueHc/";
var fileextension = ".html";
$.ajax({
//This will retrieve the contents of the folder if the folder is configured as 'browsable'
url: dir,
success: function (data) {
    //Lsit all png file names in the page
    $(data).find("a:contains(" + fileextension + ")").each(function () {
        var filename = this.href.replace(window.location.host, "").replace("http://", "").replace("https://", "");
        console.log(filename);
        filename = filename.split("/").pop();
        $("body").append($("<a href=" + dir + filename + ">" + filename + "</a>"));
    });
    });
}
});
</script>

This will log all finename(.html) to console and add hyperlink for each filename to end of body.

Rajdeep Sharma
  • 463
  • 1
  • 5
  • 9