pythongoogle-drive-apigoogle-drive-realtime-apiservice-accountsgoogle-drive-shared-drive

PYTHON Connect to Google Team Drive


I was trying to access google team drive with the service account

1) I went to team drive and add as member service account email, and granted him full access

2) I'm not sure which permission have to be granted (inside the console ) to this service account to be able to connect to google team drive

This is the coode which I'm using

def initialize_analyticsreporting():

print("Wait for Initialising")
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    "key.json", "https://www.googleapis.com/auth/drive")

# Build the service object.
analytics = build('drive', 'v3', credentials=credentials)
print(analytics)
print("Service for Google Drive - Initialised")

return analytics


def main(analytics):

    page_token = '1'
    while True:
        response = analytics.files().list(q="mimeType='image/jpeg'",
                                            spaces='drive',
                                            fields='nextPageToken, files(id, name)',
                                            pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            print("Token not initilied")
            break
    return response

if __name__ == '__main__':
    analytics = initialize_analyticsreporting()
    main(analytics)

Authentication part is working fine.

It failed as I'm not sure what is page_token.

WHAT I NEED: I need to connect to the Team Drive with this service account and get the number of files for each category, like how many videos, pictures and so on, if it possible


Solution

  • As you can see in the documentation, pageToken is the token to continue a previous request on the next page.

    Imagine you have 1000 documents in a Team Drive and you are using a pageSize of 100, in the first request you should send an empty pageToken and the response will be 100 files and a nextPageToken. If you do another files.list request using that pageToken, you will get the next 100 files from that Team drive and so on.

    So in your case, you need to send an empty pageToken instead of '1'.

    Hope it's clear!