Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to Programmatically Copy a Google Spreadsheet and Transfer Ownership from a GCP Cloud Function

I'm trying to programmatically perform the following actions from within a Google Cloud Function:

  1. Copy a Google Spreadsheet that exists in my personal Google account.
  2. Transfer ownership of the copied spreadsheet to another Google account (different email).

Here’s what I’ve tried so far:

  • Using a Service Account (with all necessary Drive scopes).
  • Using OAuth 2.0 refresh token of my personal Google account.

In both cases, the ownership transfer request fails with this error:

 

 

I/O error: 403 Forbidden
POST https://www.googleapis.com/drive/v3/files/156cD3L0-Yg_33qENsZseA6FiBHkwL9PpqYuzp7Q7whQ/permissions?sendNotificationEmail=true&transferOwnership=true
{
"code" : 403,
"errors" : [
{
"domain" : "global",
"message" : "Consent is required to transfer ownership of a file to another user.",
"reason" : "consentRequiredForOwnershipTransfer"
}
],
"message" : "Consent is required to transfer ownership of a file to another user."
}

 

 


Has anyone successfully transferred file ownership via the Google Drive API from within a Google Cloud Function, especially when dealing with personal Google accounts?

I would appreciate any help on:

  • How to handle the consentRequiredForOwnershipTransfer error.
  • Whether this kind of transfer is even allowed programmatically without manual intervention.
  • Any best practices or workarounds for this scenario?

Thanks in advance!

Solved Solved
0 1 285
1 ACCEPTED SOLUTION

Hi @aejaz,

Welcome to Google Cloud Community!

Based on the Google Drive API documentation, you can only transfer file ownership through the API if both users are in the same Google Workspace domain. It’s a security measure to keep data from being accidentally or improperly shared across unrelated accounts. However, it is mentioned in this section that you can transfer manually by updating the file permission.

Since you're working with personal Gmail accounts, the API just doesn't support ownership transfers through code—even if you're using OAuth 2.0 or a Service Account. 

You may want to try this post I saw on github using App Script to transfer file ownership. Since this site is not managed by Google, it might be outdated or incorrect.

You may also want to keep track of this feature request related to your concern.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

View solution in original post

1 REPLY 1

Hi @aejaz,

Welcome to Google Cloud Community!

Based on the Google Drive API documentation, you can only transfer file ownership through the API if both users are in the same Google Workspace domain. It’s a security measure to keep data from being accidentally or improperly shared across unrelated accounts. However, it is mentioned in this section that you can transfer manually by updating the file permission.

Since you're working with personal Gmail accounts, the API just doesn't support ownership transfers through code—even if you're using OAuth 2.0 or a Service Account. 

You may want to try this post I saw on github using App Script to transfer file ownership. Since this site is not managed by Google, it might be outdated or incorrect.

You may also want to keep track of this feature request related to your concern.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Top Solution Authors