1
votes

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 = "[email protected]",
        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

1 Answers

2
votes

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.