问题描述
在python-gdata 2.0.14中,我使用以下代码段创建和上传文档:
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
和
# 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
但是,此代码不再适用于python-gdata 2.0.16,因为 DocsClient
类不再具有Upload
和Update
函数.
However, this code does no longer work with python-gdata 2.0.16 because DocsClient
class does no more have Upload
and Update
functions.
我试图用这个
# 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)
…但是我收到此错误:
gdata.client.Unauthorized: Unauthorized - Server responded with: 401, 'Token invalid'
有人可以告诉我我的错误在哪里以及我应该如何使用该新API?
Can anybody tell me where's my mistake and how should I use that new API?
P.S. 文档尚未更新仍然使用旧式代码.
P.S. The documentation hasn't been updated and still uses the old-style code.
推荐答案
我最近也遇到了这个问题.这对我有用:
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
这篇关于如何使用新的python-gdata(2.0.16)上传文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!