Google Drive API (.NET) - Transfer ownership from Service Account to Domain user

烂漫一生 提交于 2019-12-24 07:37:28

问题


I am creating file on Google drive with .NET client API with Service account.

    string[] scopes = new string[] { DriveService.Scope.Drive };
    GoogleCredential credential;
    using (var stream = new FileStream(Directory.GetCurrentDirectory() + "/Resources/GoogleCredentials.json", FileMode.Open, FileAccess.Read))
    {
        credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
    }
    DriveService drive = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
    });

I succesfully create file,

    var f = drive.Files;
    var request = f.Create(new Google.Apis.Drive.v3.Data.File()
    {
        Name = "Test from ASP.NET Core",
        AppProperties = prop,
        MimeType = "application/vnd.google-apps.document"
    });
    var file = await request.ExecuteAsync();

share it with all domain, but I can not transfer ownership to a domain user.

    Permission permission = new Permission()
    {
        EmailAddress = "user@example.com",
        Type = "user",
        Domain = "example.com",
        Role = "owner"
    };
    var requestpermission = drive.Permissions.Create(permission, file.Id);
    requestpermission.TransferOwnership = true;
    var perm = await requestpermission.ExecuteAsync();

I get error:

The specified domain is invalid or not applicable for the given permission type.

I found this link, but using p12 cert file is not recommended. So I want to use JSON.


回答1:


Ownership transfers can only be done between users in the same domain, and service accounts don't belong to any domain. You're best option may be to create a Team Drive that the service account has access to, and perform a two stage process:

  1. Use the service account to move the file into the team drive. Files.update with the addParents parameter set to the Team Drive ID.
  2. Use the domain user to move the file out of the team drive. Files.update with the addParents parameter set to root (or some target folder's ID) and the removeParents parameter set to the Team Drive ID.


来源:https://stackoverflow.com/questions/42750785/google-drive-api-net-transfer-ownership-from-service-account-to-domain-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!