0

I am testing my python connection to Google Drive using Python Google API Client. I am able to establish a connection, but I am not getting back a list of objects from Google Drive. I wonder if a lack of permission in my API key is giving a silent error.

from googleapiclient.discovery import build, Resource

def test_gdrive(gdrive):
    #connect to google drive.
    #gdrive is a custom class that returns a Resource object from googleapiclient
    service Resource = gdrive.connect_to_service('drive', gdrive.credentials, 'v3')
    assert service is not None # this passes
    # I simply want it to list the first page of all the results in my drive.
    results = service.files().list(q="'root' in parents and mimeType = 'application/vnd.google-apps.folder'"
                                   spaces='drive', 
                                   fields='nextPageToken, files(id, name)', 
                                   ).execute()
    items = results.get('files', [])
    if not items:
        raise AssertionError(f'No files found in test_gdrive. Results is {results}')
    else:
        assert items[0]['id'] is not None

I am getting the custom error. AssertionError: No files found in test_gdrive. Results is {'files': []}

I have also tried the query f"name='{self.folder_name}' and mimeType='application/vnd.google-apps.folder' and 'root' in parents" to search for a specific folder in the root that I know exists. It is also coming up empty.

I was expecting at least one result from the API call.

The file is not shared, but I am authenticating as myself. The scope is " 'https://www.googleapis.com/auth/drive.file',"

2 Answers 2

1

To list files you need more than the scope

https://www.googleapis.com/auth/drive.file

You also need to add the scope

'https://www.googleapis.com/auth/drive.metadata.readonly'
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you got the query string wrong:

q="'root' in parents and mimeType = 'application/vnd.google-apps.folder'"

The mimetype filter application/vnd.google-apps.folder will only show the folders. It will exclude all other filetypes.

To list all files and folders in the root folder try with this query string:

q="'root' in parents"

You can also test if your query string is working or not directly in the Google's API explorer.

2 Comments

Why did I get an empty list instead of a list of folders?
The query q="'root' in parents" is also returning an empty list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.