4

I am using Python 2.7 and I am trying to upload a file (*.txt) into a folder that is shared with me.

So far I was able to upload it to my Drive, but how to set to which folder. I get the url to where I must place this file.

Thank you

this is my code so far

def Upload(file_name, file_path, upload_url):

    upload_url = upload_url
    client = gdata.docs.client.DocsClient(source=upload_url)
    client.api_version = "3"
    client.ssl = True
    client.ClientLogin(username,  passwd, client.source)

    filePath = file_path
    newResource = gdata.docs.data.Resource(filePath,file_name)

    media = gdata.data.MediaSource()
    media.SetFileHandle(filePath, 'mime/type')

    newDocument = client.CreateResource(
        newResource,
        create_uri=gdata.docs.client.RESOURCE_UPLOAD_URI,
        media=media
    )
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
Yebach
  • 1,661
  • 8
  • 31
  • 58
  • Why do files and folders duplicate when trying to upload again? How to create a folder and upload into folder? I am getting very frustrated here, please help – Yebach Oct 30 '13 at 12:26

1 Answers1

8

the API you are using is deprecated. Use google-api-python-client instead.

Follow this official python quickstart guide to simply upload a file to a folder. Additionally, send parents parameter in request body like this: body['parents'] = [{'id': parent_id}]

Or, you can use PyDrive, a Python wrapper library which simplifies a lot of works dealing with Google Drive API. The whole code is as simple as this:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

f = drive.CreateFile({'parent': parent_id})
f.SetContentFile('cat.png') # Read local file
f.Upload() # Upload it
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
JunYoung Gwak
  • 2,967
  • 3
  • 21
  • 35
  • After installing the API's I get an error pydrive.settings.InvalidConfigError: Invalid client secrets file File not found: "client_secrets.json". Where do I put json file? After putting the json in directory upload opens a browser window and demands confirmation for my app - how do I cancel that – Yebach Oct 28 '13 at 12:46
  • There's no way you don't prompt user confirmation at least once. That is how OAuth works. However, you can save credentials to reuse it after user first authorizes access. Take a look at documentations for details: http://pythonhosted.org/PyDrive/index.html – JunYoung Gwak Oct 28 '13 at 18:14
  • Since I am doing a server running script that reads from google docs creates a new document and uploads it, what do you suggest to use to solve this problem? – Yebach Oct 29 '13 at 07:11
  • I menaeged to bypass prompt login. I would like to know how to upload file to a specific folder that is shared with my – Yebach Oct 29 '13 at 11:12
  • How can I obtain the "parent_id" value I should use? Right now I am copying the id-looking part of the share folder url, but that does not work. – Maksim Gorkiy Mar 11 '19 at 12:43