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'