Reputation: 21
I try to update the owner permission of some files in google drive using google drive api v2 programmatically.(like Google Admin Console does on "Drive / Transfer Ownership" form) I created a service account and use its ID combined with a super-admin account to create credentials and "DriveService" object. I able to list all files shared with me and enumerate the "Owners" collection of "File". Adding a new permission to a file (role="writer") works fine. If I try to change the new permission to role ="owner" I get an INTERNAL ERROR 500.
Had anyone seen that error before and how can I work around?
This is my code:
// ---------------------- BUILD CREDENTIALS -------------------------------
Google.Apis.Auth.OAuth2.ServiceAccountCredential m_Creds = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(<MY_SERVICE_ACCOUNT>)
{
Scopes = new[]
{
DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveAppdata
}
,
User = <MY_ADMIN_ACCOUNT>
}.FromCertificate(certificate));
// ---------------------- BUILD CLIENTINIT -------------------------------
var myCInit = new BaseClientService.Initializer
{
ApplicationName = "Testapplication",
HttpClientInitializer = m_Creds
};
string myAccountname = "Test User1"; // accountname to look for
string myNewAccountEmail = "[email protected]"; // email of new owner account
// ---------------------- DRIVE SERVIVE CREATION -------------------------------
var myDService = new DriveService(myCInit);
// ----------------------- GET THE FILE LIST------------------------------------
var myFList = myDService.Files.List().Execute();
foreach (Google.Apis.Drive.v2.Data.File currF in myFList.Items)
{
foreach (Google.Apis.Drive.v2.Data.User currUser in currF.Owners)
{
if (currUser.DisplayName.StartsWith(myAccountname))
{
// create new permission for new owner
Permission p = new Permission();
p.Role = "writer";
p.Type = "user";
p.Value = myNewAccountEmail;
myDService.Permissions.Insert(p, currF.Id).Execute(); // works fine, creates new permission
// get permission id
var myNewID = myDService.Permissions.GetIdForEmail(myNewAccountEmail).Execute();
// get my newly created permission
p = myDService.Permissions.Get(currF.Id, myNewID.Id).Execute();
p.Role = "owner";
// create update request
var myUpdR = myDService.Permissions.Update(p, currF.Id,myNewID.Id);
myUpdR.TransferOwnership = true; // yes, we want to be an owner
myUpdR.Execute(); // this call gets an "internal error 500"
}
}
}
Any help is appreciated.
Thx. TL
Upvotes: 2
Views: 1620
Reputation: 1061
The problem is that you first of must be in the same domain for the one that is changing the ownership to the other user. This due to a security aspect.
The other one is that changing the file, it has to be pointed to a destination (folder id) and the best way I found out to work, is that the destination-users share a folder to the source-user. And then it is able to procedee with the change permission of the file.
I did it in this sequence to make this possible:
Upload file using admin-user and point it to the folderId that destination-users have shared.
InsertPermission (fileId, user.email, "user", "owner")
RemoveOwnPermission, by sending in the admin-user email and remove it on the file.
Upvotes: 0