I found that the best way to transfer the ownership is to
Enable domain wide delegation to the service account
Impersonate the user while initialling the drive service as below:
using (var credentialJSON =new FileStream(_settings.CertificatePath,FileMode.Open, FileAccess.Read))
{
var credentialParameters =
NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(credentialJSON);
credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
Scopes = scopes,
User = ownerEmailAddress, //put in the actual user email address
}.FromPrivateKey(credentialParameters.PrivateKey));
}
How can I do the same in Python? I usually create my Drive in this way:
credentials = service_account.Credentials.from_service_account_file(
service_json_fp, scopes=SCOPES)
drive_api = build('drive', 'v3', credentials=credentials)