Services 3.0 Python example using XML-RPC and OAuth2

This code connects using two-legged OAuth to a drupal host running services-3.x, OAuth and an XMLRPC endpoint. This example uses two different ways to connect - one using xmlrpclib, one not. It could probably be easily adapted for python-oauth instead of oauth2.


# Note, am using two-legged auth here.
# needs http://drupal.org/node/1336974 for two-legged and http://drupal.org/node/1337718 if using a nonstandard port.
#
# there's a couple of approaches here. One uses xmlrpclib, one doesn't. Both use oauth2 - but could probably be worked with oauth
#
# services_endpoint needs to be something like (serialiazed as I've pulled it from database and there's no form yet):
# a:1:{s:14:"services_oauth";a:3:{s:13:"oauth_context";s:n:"CONTEXT GOES HERE";s:11:"credentials";s:8:"consumer";s:13:"authorization";s:n:"DEFAULT SECURITY LEVEL GOES HERE";}}
# Don't forget to fix for 'CONTEXT GOES HERE' and 'DEFAULT SECURITY LEVEL GOES HERE' (and fix 'n' to be the right number)

import oauth2
import time
import urllib2
import xmlrpclib

host_key = 'PUT A KEY HERE'
host_secret = 'PUT A SECRET HERE'
# this is the endpoint.
host = 'https://testserver:1081/endpoint'

class OAuthTransport(xmlrpclib.SafeTransport):
def __init__(self, host, verbose = None, use_datetime=0):
xmlrpclib.SafeTransport.__init__(self)
self.verbose = verbose

Getting Your First REST Server Working

If you have never dealt with services before, then this guide may help you get a REST server on Drupal 7 up and running.

I have pulled this example module from other sources and made one small edit to it, of adding ->fetchAll() to the create function, but all you have to do is to install 1) the Services module, 2) the REST server within the Services module package, 3) this custom module below, and 4) the Firefox plugin called "Poster" (which allows you to check that everything is working). You can do this on any Drupal 7 site, as far as I know.

NOTE: The instructions written below are to get you up and running in your local environment so you understand how to make the site with a working REST server. Following these steps and not doing anything further may leave your site vulnerable.
You may also download the archive attached with files containing all the code shown here.

Here is the custom "noteresource" module to install:

noteresource.info:

name = Note Resource
description = Sample resource implementation
package = Notes example

core = 7.x

files[] = noteresource.inc
files[] = noteresource.module

noteresource.install:

<?php
// noteresource.install
/**
* Implementation of hook_schema().
*/
function noteresource_schema() {
$schema['note'] = array(
'description' => t('Stores information about notes.'),

Services

Services provides access to core functionality to remote clients (things like nodes, users, taxonomy, etc.) The following projects integrate with Services to provide the same functionality outside of what is included in core.

Pages

Subscribe with RSS Subscribe to RSS - services