1

With python-gdata 2.0.14, I used the following pieces of code to create and upload documents:

# To create a document
import gdata.docs
import gdata.docs.client
from gdata.data import MediaSource

gdClient = gdata.docs.client.DocsClient(source="my-app")
gdClient.ssl = True
gdClient.ClientLogin("login", "pa$$word", gdClient.source)
ms = MediaSource(file_path="temp.html", content_type="text/html")
entry = gdClient.Upload(ms, "document title")
print "uploaded, url is", entry.GetAlternateLink().href

and

# To update a document
entry.title.text = "updated title"
entry = gdClient.Update(entry, media_source=ms, force=True)
print "updated, url is", entry.GetAlternateLink().href

However, this code does no longer work with python-gdata 2.0.16 because DocsClient class does no more have Upload and Update functions.

I tried to use this

# Try to create a document
gdClient = gdata.docs.client.DocsClient(source="my-app")
gdClient.ssl = True
gdClient.ClientLogin("login", "pa$$word", gdClient.source)
ms = MediaSource(file_path="temp.html", content_type="text/html")
entry = gdata.docs.data.Resource(type=gdata.docs.data.DOCUMENT_LABEL, title="document title")
self.resource = gdClient.CreateResource(entry, media=ms)

… but I get this error:

gdata.client.Unauthorized: Unauthorized - Server responded with: 401, 'Token invalid'

Can anybody tell me where's my mistake and how should I use that new API?

P.S. The documentation hasn't been updated and still uses the old-style code.

1 Answer 1

1

I was having issues with this recently too. This worked for me:

import gdata.docs.data
import gdata.docs.client

client = gdata.docs.client.DocsClient(source='your-app')
client.api_version = "3"
client.ssl = True
client.ClientLogin("[email protected]", "password", client.source)

filePath = "/path/to/file"
newResource = gdata.docs.data.Resource(filePath, "document title")

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

newDocument = client.CreateResource(newResource, create_uri=gdata.docs.client.RESOURCE_UPLOAD_URI, media=media)

Edit: Added the packages to import to avoid confusion

2
  • Thanks! I still get some errors with Python 3 version (which I built using 2to3), but I'll try to sort those out later. Feb 6, 2012 at 15:04
  • Hello. Your example kind of works for me but instead of document being placed in the folder I want (or should I say to the url I give) in is placed to my account. the document is suppose to be seen for everybody inside a group
    – Yebach
    Oct 23, 2013 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.