Reputation: 31109
I developing an application which works with Google API, according to my understanding.
def push_to_ga(request):
client = gdata.docs.service.DocsService()
client.ClientLogin('[email protected]', 'password')
entrys = Entry.objects.all()
for entry in entrys:
splitted = entry.file.split('/')
client.UploadDocument(entry.file, splitted[-1])
return HttpResponseRedirect('https://docs.google.com/#home')
Have an error:
Traceback: File "/home/i159/Env/googleapi/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/i159/workspace/apiroot/googleapi/../googleapi/apiapp/views.py" in push_to_ga 38. client.UploadDocument(entry.file, 'My entry #' + str(entry.id)) File "/home/i159/Env/googleapi/lib/python2.6/site-packages/atom/init.py" in deprecated_function 1475. return f(*args, **kwargs) File "/home/i159/Env/googleapi/lib/python2.6/site-packages/gdata/docs/service.py" in UploadDocument 494. folder_or_uri=folder_or_uri) File "/home/i159/Env/googleapi/lib/python2.6/site-packages/gdata/docs/service.py" in _UploadFile 160. extra_headers={'Slug': media_source.file_name},
Exception Type: AttributeError at /push_to_ga/ Exception Value: 'unicode' object has no attribute 'file_name'
I can't find docs with description of the methods. How to upload file into Google Docs through API?
Upvotes: 0
Views: 773
Reputation: 5919
Which version of the Google API are you using?
According to the Google documentation, for versions 1.0 and 2.0 you have to wrap the document as a MediaSource object in order to pass it to the Upload method. So, I think you need to replace:
client.UploadDocument(entry.file, splitted[-1])
with:
ms = gdata.MediaSource(file_path=entry.file, content_type=gdata.docs.service.SUPPORTED_FILETYPES['DOC'])
client.Upload(ms, splitted[-1])
Note: this assumes that you are uploading Word files. You should set the content_type
parameter to the correct type for each file that you upload.
If you're using version 3.0, you no longer need to create a MediaSource object - you can simply pass the pathname, title and mime type directly to the Upload method:
client.Upload(entry.file, splitted[-1], content_type='application/msword')
Uploading PDFs
If you attempt to upload PDF files using version 2.0 of the API, it fails with the error:
{'status': 415, 'body': 'Content-Type application/pdf is not a valid input type.', 'reason': 'Unsupported Media Type'}
This can be fixed using the workaround shown in comment 77 on issue 591 on the Google Code site. Simple edit the _UploadFile
method in your site-packages/gdata/docs/services.py
file as shown on that ticket. Once you have made this change, PDF uploads should work fine (I have checked this & it works for me).
Upvotes: 1