nEO
nEO

Reputation: 5405

Get contents of a folder in Google Drive using Python & APICLient

I'm trying to get the contents of a Google Drive folder using the apiclient module in Python.

Here's my code

    #!/usr/bin/env python

    from __future__ import print_function
    import os

    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    import requests
    try:
        import argparse
        flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
    except ImportError:
        flags = None

    SCOPES = 'https://www.googleapis.com/auth/drive.file'
    store = file.Storage('storage.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
        creds = tools.run_flow(flow, store, flags) \
                if flags else tools.run(flow, store)
    DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
    print (DRIVE.children().list( folderId='# id of the folder').execute()['items'])

However, all I get it an empty list instead of a populated one. I know the folder has files inside it.

I'm referencing the code here. Google Drive folder ID

Suggestions

Upvotes: 1

Views: 1083

Answers (1)

abielita
abielita

Reputation: 13469

I guess you did not include creds = tools.run_flow(flow, store, flags) in your if-statement.

if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
        creds = tools.run_flow(flow, store, flags) \
                if flags else tools.run(flow, store)

This should be:

if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)

        if flags:
            creds = tools.run_flow(flow, store, flags)
        else:
            creds = tools.run(flow, store)

You can find it in this Google documentation.

Upvotes: 1

Related Questions