4

I'm trying to use Google-drive-api and I have a problem. I followed accurately every instructions of the quickstart tutorial for google Drive api and I have an error with a file that I don't have. Python quickstart tutorial Here is my code :

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

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

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive.metadata'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'


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-python-quickstart.json')

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

def main():
    """Shows basic usage of the Google Drive API.

    Creates a Google Drive API service object and outputs the names and IDs
    for up to 10 files.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    results = service.files().list(
        pageSize=10,fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])
    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print('{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()

Result :

C:\Python27\lib\site-packages\oauth2client_helpers.py:255: UserWarning: Cannot access C:\Users\Neyoh.credentials\drive-python-quickstart.json: No such file or directorywarnings.warn(_MISSING_FILE_MESSAGE.format(filename))

I have the client_secret.json file in the directory of my python script but in HOME/.credentials/ , I have nothing. The tutorial speaks about client_secret.json but not about drive-python-quickstart.json . It's never mentioned. What is drive-python-quickstart.json ? Is the same file ?

Edit1 : When I use client_secret.json in \.credentials\ instead of drive-python-quickstart.json I have this error :

File "C:\Python27\lib\site-packages\oauth2client\client.py", line 302, in new_from_json module_name = data['_module'] KeyError: '_module'

2
  • @DaImTo When i used client_secret.json , I have this error: File "C:\Python27\lib\site-packages\oauth2client\client.py", line 302, in new_from_json module_name = data['_module'] KeyError: '_module' Commented Nov 11, 2016 at 12:19
  • I found, drive-python-quickstart.json must be an empty file. Commented Nov 14, 2016 at 20:45

1 Answer 1

5

I went into the same issue with google calendar api. To solve the problem simply use the absolute file path to your CLIENT_SECRET_FILE like:

CLIENT_SECRET_FILE = r'C:\Users\xxx\client_secret.json'

The file drive-python-quickstart.json will be created after authentication automatically.

Sign up to request clarification or add additional context in comments.

Comments

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.