SkyDancer
SkyDancer

Reputation: 159

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

Upvotes: 4

Views: 4374

Answers (3)

Marek Bern&#225;d
Marek Bern&#225;d

Reputation: 629

For those looking for a simple Python solution here as me:

# upload file to drive
uploaded_file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()

# set permission - toAll - for file in drive
drive_service.permissions().create(fileId=uploaded_file['id'], body={'role': 'reader', 'type': 'anyone'}).execute()

Upvotes: 0

mcanic
mcanic

Reputation: 11

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

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

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

Upvotes: 2

Related Questions