Traxys
Traxys

Reputation: 95

Limits with Google Drive API

Someone I know wanted a programm that listed all google drive ids of the files in a certain folder. So I looked on the google website , learned a bit on how to do it in python and came up with a programm. The problem is that there seems to be a limit , but one I can't quite understand :

If I use this code :

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)

par = {'maxResults':1000}
results = service.files().list(**par).execute()
items = results.get('items', [])
folders = {}
if not items:
    print('No files found.')
else:
    print("There is "+str(len(items))+" items")

Where I took the get_credentials() on a sample of the google developper website , the thing is that I know for sure that the drive I am acessing has more than a 1000 files , still the programm tells me There is 460 items , is there any way this is logic that it limits to 460 ?

Upvotes: 2

Views: 1152

Answers (2)

Sebastien Chartier
Sebastien Chartier

Reputation: 130

I have the same issue, I keep receiving 460 results even if I set maxResults to 1000...

According to the documentation: "maxResults: Maximum number of files to return. Acceptable values are 0 to 1000, inclusive. (Default: 100)"

https://developers.google.com/drive/v2/reference/files/list

Upvotes: 2

seanpj
seanpj

Reputation: 6755

I don't do Python, but I think you're missing the 'nextPage' logic. See, (at least in Java) the list returns one 'page' of results at a time, so there has to be another loop that retrieves the next page (as long as one is available), like:

  void list(String prnId) {
      String qryClause = "'me' in owners and '" + prnId + "' in parents";
      Drive.Files.List qry = mGOOSvc.files().list()
          .setQ("'me' in owners and '" + prnId + "' in parents")
          .setFields("items, nextPageToken");
      String npTok = null;
      do {
        FileList gLst = qry.execute();
        if (gLst != null) {
          // BINGO - file list, but only one page worth
          npTok = gLst.getNextPageToken();  //GET POINTER TO THE NEXT PAGE
          qry.setPageToken(npTok);
        }
      } while (npTok != null && npTok.length() > 0);
  }

See the 'do while()' loop and the 'BINGO' comment? Try to implement it to your PY code (and don't forget to ask for 'nextPageToken' to get it).

Also, there is a great tool, TryIt! on the bottom of this page, where you can test all kinds of stuff.

Good Luck

Upvotes: 2

Related Questions