How do I get a file list for a Google Drive public

2019-02-09 10:33发布

问题:

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.

回答1:

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



回答2:

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(){

});


回答3:

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



回答4:

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.