I am new to using Google Drive API and have a lot of trouble understanding how things work with that API. After searching all over the web, I finally found a working example here and got the following code to work for me:
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
#Set up a credentials object I think
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', ['https://www.googleapis.com/auth/drive'])
#Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)
file_name = "test"
print("Uploading file " + file_name + "...")
#We have to make a request hash to tell the google API what we're giving it
body = {'name': file_name, 'mimeType': 'application/vnd.google-apps.document'}
#Now create the media file upload object and tell it what file to upload,
#in this case 'test.html'
media = MediaFileUpload('test.csv', mimetype = 'text/csv')
#Now we're doing the actual post, creating a new file of the uploaded type
fiahl = drive_api.files().create(body=body, media_body=media).execute()
#Because verbosity is nice
print("Created file '%s' id '%s'." % (fiahl.get('name'), fiahl.get('id')))
When I run the above code, I get the following:
Uploading file test...
Created file 'test' id '12kV2WUPg64_asVgVaVFF0X3lFAA_RkVJGtL-eDnexxM'.
Which means the code executed successfully so I am expecting to see test.csv somewhere on my Google Drive but that is not the case and that file is no where to be found. So, couple of questions:
- Where should I be looking for that file?
- Is there anyway to specify a directory in the Google Drive so the code would put the file in that specific directory?
Any help is much appreciated because it is almost impossible to find all working peaces of this Google Api together in one place.