18

I am working on Google drive application which will allow user to create file which must be public.

I could see some example where we can create a file in Google drive through APIs.

But, While creating a file, Is it possible to share a file as public.

Mohammed H
  • 6,880
  • 16
  • 81
  • 127
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153

2 Answers2

30

You can set the Access Control List of the file using the Permissions feed. The documentation is located here:

https://developers.google.com/drive/v2/reference/permissions

To make a file public you will need to assign the role reader to the type anyone

Then, if you want a link to share to people, you can grab the webContentLink URL returned in the File metadata in the API, it will allow any users to download the file. You can also use it to embed the shared file into HTML (for instance images in <img> tags) .

Nicolas Garnier
  • 12,134
  • 2
  • 42
  • 39
  • 1
    When making a file public using permissions, you also need to set 'value', which is described in the documentation as "the email address or domain name for the entity." What should 'value' be when you want the file to be publicly available using 'webContentLink' and you do not want to require that the user be logged in to Google Drive? – user1501783 Jul 31 '12 at 20:49
  • 1
    The value can be an empty String: '' – Nicolas Garnier Aug 06 '12 at 17:58
  • Answer is right but when you grab the sharing link, its better to use something like `https://drive.google.com/open?id={your ID}`. This is because If the shared item is a directory, `WebContentLink` will be empty. Morover, `WebContentLink` gives a direct download link, for which no online preview will be available. – Divins Mathew Dec 29 '16 at 13:44
7

I think that would be nice to show the code example based on the answer which provided by Nivco. Using Javascript you can do it like that:

var google = require('googleapis');
var _ = require('lodash-node/compat');
var Q = require('q');   
var OAuth2 = google.auth.OAuth2; 


var CLIENT_ID = '...';
var CLIENT_SECRET = '...';
var REDIRECT_URL = '...';

var shareFile = function (fileName) {
  var deferred = Q.defer();
  var drive = google.drive('v2');
  var auth = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

  drive.files.list({auth: auth}, function (err, res) {
    var foundFile = _.first(_.filter(res.items, {title: fileName, "explicitlyTrashed": false}));

    if (!foundFile) {
        deferred.reject('File ' + fileName + ' has not been found.');
        return;
    }

    drive.permissions.list({fileId: foundFile.id, auth: auth}, function (err, res) {

        if (_.isEmpty(_.find(res.items, 'role', 'reader'))) {
            var body = {
                'value': 'default',
                'type': 'anyone',
                'role': 'reader'
            };

            drive.permissions.insert({
                fileId: foundFile.id,
                resource: body,
                auth: auth
            }, function (err, res, body) {
                deferred.resolve(body);
            });
        }
    });
});
return deferred.promise;

};

Bogdan Nechyporenko
  • 1,226
  • 2
  • 14
  • 21