Community Documentation

Example: Authenticate user and create node using Services 3.0 XML-RPC and Python

Last updated January 17, 2012. Created by josepvalls on January 17, 2012.
Log in to edit this page.

After you have exposed the node and user resources you can first log in a user and perform an action with the session cookie returned. To keep the session cookie I am using some code from Roberto Rocco Angeloni:
http://www.roccoangeloni.it/wp/2008/06/13/xmlrpclib-with-cookie-aware-tr...

Then:

tr = CookieTransport()
server = xmlrpclib.Server(url, transport=tr)
server.system.connect()
user = server.user.login('user','password')
tr.SESSION_ID_STRING = user['session_name']
tr.mysessid = user['sessid']
new_node = {'body': 'Ordenar bibliotecas es ejercer de un modo silencioso el arte de la critica.\n-- Jorge Luis Borges. (1899-1986) Escritor argentino.', 'type': 'page', 'title': 'Just a little test'}
server.node.create(new_node)
server.node.retrieve(10)['uid']

Comments

drupal_services.py updated

I wrote this page after I found out that the linked drupal_services.py project was outdated.
I found an updated project on github and I've updated the documentation in http://drupal.org/node/763066
https://github.com/dgtlmoon/python_drupal_services/blob/master/drupal_se...

Service 3

hello I launched the updated drupal_services.py (node creation test) with Service 3.1 and I get "Access denied for user anonymous" even though I'm using admin credentials, does it work for you ?

my quick fix for

my quick fix for drupal_services.py
line 57
- result = re.search('.+(SESS.*?;)', response.getheader("Set-Cookie", 0), re.IGNORECASE)
- if result is not None:
- self.cookie = result.group(1)
+ self.cookie = response.getheader("Set-Cookie", 0)

also line 97
- return getattr(self, method_name)(args[0])
+ return getattr(self, method_name)(*args)

example?

I am pretty new to python, do you mind giving some example code of how to use this class.
Thank you in advance.

About this page

Drupal version
Drupal 6.x
Audience
Programmers
Level
Intermediate
Keywords
python, services

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.