Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google drive api, upload file with shared permission

Im trying to upload file to the google drive, and I want to set 'shared' permission for this file, but I don't know how to do it... I tried to use this code, but the file is uploaded without the shared permission.

My code is:

// _drive - google drive object
Google.Apis.Drive.v2.Data.File item = new Google.Apis.Drive.v2.Data.File();

Permission permission = new Permission();
permission.Role = "reader";
permission.Type = "anyone";
permission.WithLink = true;

item.Permissions = new List<Permission>() { permission };
FilesResource.InsertMediaUpload request = _drive.Files.Insert(item, fileStream, mimeType);
request.Upload();
like image 781
SkyDancer Avatar asked Nov 06 '25 08:11

SkyDancer


2 Answers

OK I have spent the last hour playing around with this. If you check out Files.insert documentation it doesn't really state anyplace that you should be able to set the permissions at insert time.

At the bottom if you test out try it. Setting the permissions up as you have done above under Request body.

enter image description here

It does upload the file. But the Json returned gives us a clue.

"shared": false,

Now if i check the file in Google drive

enter image description here

This leads me to believe that this is not supported by the Google Drive API. It is not possible to set the permissions at the time of upload. You are going to have to create a separate call to set the permissions after you have uploaded the file.

While it looks like the body does support the permissions it doesn't appear to be working. I am not sure if this is a bug or something that is just not supported. I am going to see if i can find the location of the issue tracker for Drive and add it as an issue.

In the mean time you are going to have to make the two calls and eat a bit of your Quota.

Issue 3717: Google drive api, upload file with shared permission

like image 54
DaImTo Avatar answered Nov 08 '25 10:11

DaImTo


Also experienced this bug. Specifying permissions in the same file / directory upload, had to do it in a separate request, like below. The Google Drive API documentation is not clear about this (and not clear about how to handle file permissions when using a Service Account).

var NewDirRequest = DService.Files.Insert(GoogleDir);
var NewDir = NewDirRequest.Execute();
GoogleFolderID = NewDir.Id;

var NewPermissionsRequest = DService.Permissions.Insert(new Permission()
{
    Kind = "drive#permission",
    Value = emailAddress,
    Role = "writer",
    Type = "user"
}, GoogleFolderID);

DService.Permissions.Insert(new Permission()
{
    Kind = "drive#permission",
    Value = "mydomain.com",
    Role = "reader",
    Type = "domain"
}, GoogleFolderID);

NewPermissionsRequest.Execute();
like image 30
mcanic Avatar answered Nov 08 '25 08:11

mcanic