A client has asked me to develop a script to automatically change the owner of the files contained in a folder. Since GAS does not allow the change of owners to the files of other users I tried to do it with "Delegating domain-wide authority to the service account" of the guide [ link Using the following code:
def main():
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = credentials.with_subject('[email protected]')
service = build('drive', 'v3', credentials=delegated_credentials)
files = service.files().list().execute()
file_list = [each.get('id') for each in files.get('files')]
for file_id in file_list:
per = service.permissions().list(fileId=file_id).execute()
edit_id = ''
for each in per['permissions']:
if each.get('role') == 'writer':
edit_id = each.get('id')
permission = service.permissions().update(
fileId=file_id, permissionId=edit_id, transferOwnership='true',
body={'role': 'owner'}).execute()
Result:
googleapiclient.errors.HttpError: https://www.googleapis.com/drive/v3/files/1YEHKl4c-EeHEaMdcWDa9YhSZFXYDxFDZ/permissions/15874118146408716 691? Alt = json & transferOwnership = true returned "The user does not have enough permissions for this file." >
Any suggestions? Thanks in advance.