0

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:

  1. Where should I be looking for that file?
  2. 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.

ahajib
  • 12,838
  • 29
  • 79
  • 120
  • 1
    have you see this full example ? https://github.com/samlopezf/google-drive-api-tutorial/blob/master/google-drive-api-tutorial-project/main.py – GiovaniSalazar Nov 25 '19 at 17:29

1 Answers1

1

You're using a Service Account so the file has been created in the Drive belonging to that account, not in your personal account.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Thanks for you answer. Could you provide more details please? I've used my personal email to create the credentials used for uploading files. I can only see console.developers.google.com/apis/dashboard and not sure where to look for the uploaded files – ahajib Nov 25 '19 at 18:14
  • If you used your personal email, then the files should be in your personal Drive. – Jescanellas Nov 26 '19 at 08:16
  • @Jescanellas your comment is incorrect. It does not matter which email you used to create the Service Account. The point is that the Service Account is *not* your account. This question has been answered many time on SO, for example https://stackoverflow.com/questions/28534822/location-of-google-drive-service-account-uploads – pinoyyid Nov 26 '19 at 19:36