Python 3 - Google Drive API: AttributeError: 'Resource' object has no attribute 'children'

10,907

It seems you have recently upgraded your API version from 2 to 3! Per the Drive API changelog, there is no children() resource anymore. I suspect there are other changes you aren't expecting, so be sure to review that changelog.

Some helpful info via the Python Client Library documentation for Drive V3:

about() Returns the about Resource.
changes() Returns the changes Resource.
channels() Returns the channels Resource.
comments() Returns the comments Resource.
files() Returns the files Resource.
permissions() Returns the permissions Resource.
replies() Returns the replies Resource.
revisions() Returns the revisions Resource.
teamdrives() Returns the teamdrives Resource.
new_batch_http_request() Create a BatchHttpRequest object based on the discovery document.

If you don't want to migrate, there is still a children() resource for Drive V2:

about() Returns the about Resource.
apps() Returns the apps Resource.
changes() Returns the changes Resource.
channels() Returns the channels Resource.
children() Returns the children Resource.
comments() Returns the comments Resource.
files() Returns the files Resource.
parents() Returns the parents Resource.
permissions() Returns the permissions Resource.
properties() Returns the properties Resource.
realtime() Returns the realtime Resource.
replies() Returns the replies Resource.
revisions() Returns the revisions Resource.
teamdrives() Returns the teamdrives Resource.
new_batch_http_request() Create a BatchHttpRequest object based on the discovery document.

Your solution, then, is to either build the V2 version of the Drive REST API:

service = build('drive', 'v2', ...)

or continue using v3 and update your code to use the files() resource as is now required.

You can request the children of a folder having id folderId with the proper arguments and calling list and list_next:

Python3 code:

kwargs = {
  "q": "'{}' in parents".format(folderId),
  # Specify what you want in the response as a best practice. This string
  # will only get the files' ids, names, and the ids of any folders that they are in
  "fields": "nextPageToken,incompleteSearch,files(id,parents,name)",
  # Add any other arguments to pass to list()
}
request = service.files().list(**kwargs)
while request is not None:
  response = request.execute()
  # Do stuff with response['files']
  request = service.files().list_next(request, response)

References:

Share:
10,907

Related videos on Youtube

Alve
Author by

Alve

i use arch btw

Updated on June 04, 2022

Comments

  • Alve
    Alve almost 2 years

    I made a command-line folder selector. I want it to list all files in the folder. I've tried by using the service.children()-thing but I can't get that to work. The thing that doesn't work:

    files = service.children().list(folderId=file_id).execute()
    

    Here is the code instancing service object:

    service = build('drive', 'v3', http=creds.authorize(Http()))
    

    Other parts of the code works, so I know that the service is working

    I know that the variable file_id is a valid folder. Someone who knows that it might be?

  • Matias Haeussler
    Matias Haeussler about 5 years
    In my case I had to use "'{}' in parents" (check ' ' around {}).