Search code examples
pythonherokugoogle-drive-apigoogle-oauth

What credentials do I need to manage my own Drive with Google Drive API from a web app?


I am running a web app on heroku, and my goal is to copy files within my own drive using Google Drive API for python by sending commands to the app.

So far I have this code:

import os
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from apiclient import discovery

# use creds to create a client to interact with the Google Drive API
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_dict(json.loads(os.environ.get('CREDENTIALS')), scope)
client = gspread.authorize(creds)


http = creds.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http, cache_discovery=False)

folder = "12vQQwYK5bkg-6UKoNrXpsU1C1-fiYeTX"  # folder ID
file = "1EA25-BYr1AAUUDcstfVowDeGoygMGuxKxGxFYEdKTX0"  # file ID
title = "New_file_id"

service.files().copy(fileId=file,
                           body={"parents": [{"kind": "drive#fileLink",
                                 "id": folder}], 'title': title}).execute()

But it gets stuck on executing POST request forever. What am I doing wrong?


Solution

  • When you use Drive API v3, please use name to give the filename instead of title. And please put kind to the outside of parents. So how about the following modification for body?

    From :

    {
        "parents": [{"kind": "drive#fileLink", "id": folder}],
        'title': title
    }
    

    To :

    {
        "parents": [folder],
        "name": title,
        "kind": "drive#fileLink"
    }
    

    Reference :

    If this didn't lead to the solution, I'm sorry.