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 ) 
1
  • 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 Commented Oct 30, 2013 at 12:26

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

5 Comments

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
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: pythonhosted.org/PyDrive/index.html
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?
I menaeged to bypass prompt login. I would like to know how to upload file to a specific folder that is shared with my
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.