Understanding how to use the Google API client library API with Python

To improve my Python skills, I tried to read and understand the source code of the Google Python API .
But I was stuck and inspired by the search for trips, I can not understand the work of any part of the code.

I made a small program to demonstrate this part:

upload.py

from __future__ import print_function
import os
import httplib2

import apiclient
import oauth2client

try:
    import argparse
    flags = argparse.ArgumentParser(
        parents=[oauth2client.tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'

# Enter your project name here!!
APPLICATION_NAME = 'API Project'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'drive-credentials.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = oauth2client.client.flow_from_clientsecrets(
            CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = oauth2client.tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = oauth2client.tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())

    file_service = apiclient.discovery.build('drive', 'v3', http=http).files()

    results = file_service.get(
        fileId="0Bw239KLrN7zoWl95Nml2ZUpsNnc").execute()
    print(results)

    results = file_service.list(
        pageSize=10, fields="files(id, name)").execute()
    print(results)

if __name__ == '__main__':
    main()

In the line, file_service = apiclient.discovery.build('drive', 'v3', http=http).files()I can’t find the method definition files()anywhere in the library source code. I also cannot find any methods called get()or list().

I read the source code of the library in the Github repository , as well as the code documentation , but could not find anything useful.

Here is what I have tried so far:

discovery.py, build() build_from_document(), , , Resource().

, Resource() , files().

, files(), get(), list() ..

+4
2

( 2017) ! , , API Google. . , .

API Google. (, Python pip install -U google-api-python-client [ pip3 Python 3].) list() - () main(), .

# authorization boilerplate code
SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
    creds = tools.run_flow(flow, store)

# call files().list() to display 1st 100 files in My Drive folder
DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))
files = DRIVE.files().list().execute().get('files', [])
for f in files:
    print(f['name'], f['mimeType'])

(, ..), API- Drive Python... :

(*) - TL; DR: , / Google, PDF. Drive API v2, ; Drive API v3, , " ".

, : , ( ), - (apiclient, oauth2client ..).). . , DRIVE apiclient.discovery.build(), , .files() .

Drive - , , ** * API, ( .files() build()). , API ( files()), about().get(), files().list(), files().export(), .. :

: , - https://www.googleapis.com/auth/drive - , . MIMEtypes , . , , ? . , , , . , .

, storage.json .credentials/drive-credentials.json

+2

wescpy :

, storage.json, google.

drive.readonly, drive, , readonly, , storage.json.

, , storage.json script ( ) storage2.json, drive.

0

Source: https://habr.com/ru/post/1623926/


All Articles