How to transfer ownership of a file automatically?

1

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.

    
asked by Yunesky Del Río 22.02.2018 в 13:41
source

1 answer

0

Using regular user accounts you can only change the owner of the Google editors files (Documents, Sheets, Presentations, Drawings, Scripts) but not "loaded" files (PDF, DOCX, PNG, MP3, etc,)

In the case of files from Google publishers, using regular accounts only the owner, either manually or through a script, only owner can make the change of owner.

Using G Suite accounts and " domain-wide authority delegation " ("delegation of authority at the domain level "?) it is possible to change the owner of a user to another user of the domain, but it is not possible for a user of a domain other than another user of the same domain or vice versa.

An alternative is to make copies of the files you do not own.

    
answered by 22.02.2018 в 20:10