Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to share a file publicly through Google Drive API

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.

like image 251
Fizer Khan Avatar asked Jul 26 '12 08:07

Fizer Khan


People also ask

Is there an API for Google Drive?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.

Can Google Drive visitors upload files?

Visitors can edit, comment on, or view shared documents and can create new files with Docs, Sheets, Slides, and Forms in shared drive folders. Visitors with contributor access can also upload files and folders to shared drive folders. Important: Visitors can't own data.

Can you share files on Google Drive with non Google users?

Google Accounts don't have to use a gmail.com address. You can associate any existing email address with a Google Account. Alternatively, files can be shared with non-Google accounts using visitor sharing.


2 Answers

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) .

like image 64
Nicolas Garnier Avatar answered Oct 13 '22 03:10

Nicolas Garnier


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;

};

like image 43
Bogdan Nechyporenko Avatar answered Oct 13 '22 05:10

Bogdan Nechyporenko