Uploading files to Google Drive using a service account: Why can't I see the files?

Hey everyone,

I’m having trouble with my Python script that’s supposed to upload files to Google Drive using a service account. Here’s what I’ve done so far:

  1. Downloaded the JSON credentials file from the Google Developer Console
  2. Set up the Drive API service using those credentials
  3. Attempted to upload a file with the files().create() method

Although the code executes without any errors, I can’t locate the uploaded file in my Drive. I’m wondering if I’m overlooking some configuration?

Here is a revised version of my script:

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

credentials = service_account.Credentials.from_service_account_file('service_account.json')
drive_api = build('drive', 'v3', credentials=credentials)

metadata = {'name': 'example.txt'}
media_file = MediaFileUpload('example.txt', resumable=True)
result = drive_api.files().create(body=metadata, media_body=media_file, fields='id').execute()

print(f'Uploaded file ID: {result.get("id")}')

Can anyone point out what might be causing my issue? Thanks a lot!

I’ve encountered a similar issue before. The problem likely stems from using a service account, which operates independently from your personal Google account. When you upload files with a service account, they’re stored in that account’s Drive, not your personal one.

To resolve this, you have a couple options:

  1. Share the service account’s email (found in your JSON file) with your personal Google account, granting it access to the uploaded files.

  2. Use the drive.files().create() method with the ‘parents’ parameter to specify a folder ID where you want the files to appear. Ensure this folder is shared with both your personal account and the service account.

Remember, service accounts are designed for server-to-server interactions, so this behavior is actually by design. If you’re developing a personal tool, you might want to consider using OAuth 2.0 instead for a more seamless experience with your personal Drive.

I’ve dealt with this exact problem before, and it can be pretty frustrating. The key thing to understand is that service accounts have their own separate Google Drive storage. When you upload files using a service account, they’re going into that account’s Drive, not your personal one.

Here’s what worked for me:

  1. I created a folder in my personal Google Drive and shared it with the service account email (you can find this in your JSON file).

  2. Then I modified my Python script to specify this shared folder as the parent when uploading. You can do this by adding a ‘parents’ field to your metadata dict:

    metadata = {‘name’: ‘example.txt’, ‘parents’: [‘shared_folder_id’]}

Replace ‘shared_folder_id’ with the actual ID of your shared folder.

This way, the files will show up in a folder you can actually access. It’s a bit of a workaround, but it gets the job done. Hope this helps!

hey alice, i had this issue too. the files are probably in the service account’s drive, not yours. try sharing the service account email (in the JSON file) with ur google account. or use ‘parents’ parameter in create() to put files in a shared folder. good luck!