0

I am trying to use the Google Drive API in Python. I tried Google's Quickstart example I followed the exact steps mentioned there: create a project, enable the drive API, download the credentials file. I have made a separate folder for my project. After running the code I get the following error:

Error:

c:\users\yyy\appdata\local\programs\python\python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access token.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
usage: ipykernel_launcher.py [--auth_host_name AUTH_HOST_NAME]
                             [--noauth_local_webserver]
                             [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                             [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\Shahab Ali\AppData\Roaming\jupyter\runtime\kernel-dc7f13dc-e235-4dfe-baec-6f72071beaca.json

Code that I am using (shows basic usage of the Drive v3 API):

  # Creates a Drive v3 API service and prints the names and ids of the last 10 files the user has access to.
  from __future__ import print_function
  from apiclient.discovery import build
  from httplib2 import Http
  from oauth2client import file, client, tools

  # Setup the Drive v3 API
  SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
  store = file.Storage('token.json')
  creds = store.get()
  if not creds or creds.invalid:
     flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
     creds = tools.run_flow(flow, store)
     service = build('drive', 'v3', http=creds.authorize(Http()))

  # Call the Drive v3 API
  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']))

There are some post having the same heading, but the code there is different, perhaps an older version.

I tried renaming the file credentials.json to token.json and got this error:

KeyError                                  Traceback (most recent call last)
<ipython-input-3-8339ac06db15> in <module>()
     27 SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
     28 store = file.Storage('token.json')
---> 29 creds = store.get()
     30 if not creds or creds.invalid:
     31     flow = client.flow_from_clientsecrets('credentials.json', SCOPES)

c:\users\shahab ali\appdata\local\programs\python\python36\lib\site-packages\oauth2client\client.py in get(self)
    405         self.acquire_lock()
    406         try:
--> 407             return self.locked_get()
    408         finally:
    409             self.release_lock()

c:\users\shahab ali\appdata\local\programs\python\python36\lib\site-packages\oauth2client\file.py in locked_get(self)
     52 
     53         try:
---> 54             credentials = client.Credentials.new_from_json(content)
     55             credentials.set_store(self)
     56         except ValueError:

c:\users\shahab ali\appdata\local\programs\python\python36\lib\site-packages\oauth2client\client.py in new_from_json(cls, json_data)
    300         # Find and call the right classmethod from_json() to restore
    301         # the object.
--> 302         module_name = data['_module']
    303         try:
    304             module_obj = __import__(module_name)

KeyError: '_module'
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Shahab Ali
  • 389
  • 1
  • 3
  • 23
  • What's the filename of where you token is stored? Is it credentials.json or token.json ? – ReyAnthonyRenacia Jul 26 '18 at 07:23
  • my credentials.json file contains: client_id, project_id, auth_uri, token_uri, auth_provider_x509_cert_url, client_secret, redirect_uris, I think token_uri is the one, if I am correct? – Shahab Ali Jul 26 '18 at 14:09
  • ur correct but ur code snippet uses token.json ... rename ur file to "token.json" – ReyAnthonyRenacia Jul 26 '18 at 14:13
  • `token.json` is where the API client will serialize the access & refresh tokens. `credentials.json` is where the client ID / secret and refresh URIs, etc. are to be located. @noogui – tehhowch Sep 26 '18 at 16:13
  • This looks like an issue with IPython barfing when the API client warns about the JSON file being absent. It's totally fine for `token.json` to be absent - `oauth2client` is simply issuing a warning that a file it was told to use doesn't exist yet - it will create that file later if it doesn't exist. Perhaps IPython is set to treat `UserWarning` as errors? – tehhowch Sep 26 '18 at 16:15

0 Answers0