Bash script example for custom services action with session authentication

Here is Bash script example how to work with custom services action that requires session authentication.

Endpoint requirements :

Services 7.x-3.3, REST Server using 2-Legged OAuth in PHP, OAuth 7.x-3.x-dev

The Drupal REST services is an user-created module for Drupal 7 and 6.

The module converts resources on Drupal sites - content types - into REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF and other types for easy parsing by external applications.

It can also enable Drupal sites to accept CRUD operations to modify resources.

The requests between Drupal and external applications can have authentications via OAuth.

Installation

  1. Install Services module 7.x-3.3 - http://drupal.org/project/services
  2. Install Services Views module 7.x-1.0-beta2 - http://drupal.org/project/services_views
  3. Enable both modules and the REST server module.
  4. Install OAuth 7.x-3.0+18-dev - http://drupal.org/project/oauth (You must use this version because the recommended version - 7.x-3.0 - is bugged)

Note:
The Libraries module must be at least version 7.x-2.0 for REST server.

Config

This is a two-stage process.
First, make a Service Endpoint that hosts REST resources; it is seen as the first part of the URL to your resource.
Next, make a REST resource, which is a View customized by the Services View module; in it, you specify which Content Types are included.
Authentication is configured afterwards.

Config Endpoint / View without OAuth

Testing a three-legged and two-legged OAuth REST, using Services 7.x-3.2 and Oauth 7.x-3.0, with PHP Client

Here I will post all my system settings and configurations to test it out.

Prerequisite

System

  • CentOS 6.3
  • PHP 5.3.18
  • PECL Oauth extension (via command pecl install oauth)
  • Drupal-7.17
  • Server URL: http://core.zeus.lan (you can use any URL BUT You SHOULD really going for HTTPS)
  • Client URL: http://localhost/oauth.php (this will be used below)

Modules

Step-by-Step

I use location sites/all/modules/contrib for my drupal modules.

Services Configuration

First we set up the services with oauth.

  1. In Drupal site, install modules: REST Server, OAuth Authentication, OAuth Provider UI.
  2. In Drupal directory, comment out line 6 to 8 in file oauth/lib/OAuth.php, since it will conflict with PECL OAuth.
  3. In Drupal site, Create OAuth context in admin/config/services/oauth/add, make sure to create Authorization level and set as default.

Testing a three legged OAuth using Services 7.x-3.x XMLRPC

After a couple of days bashing my head against my desk, I have finally got a C client to talk to Drupal 7.x using Services 3.x (XMLRPC) and OAuth authentication. Previously I had been using Drupal 6.x with Services 2.x and an API key, but the API key option is not available in Services 3.x. I looked for documentation explaining how to set up Services 3.x with OAuth, but I couldn't find a step-by-step tutorial. When I finally worked it out, I decided I'd better write one so that others don't have to follow my path of pain.

API documentation

Services 3.x - API Documentation

Formal API documentation is underway for all the hooks being exposed by Services 3.x, including the keys and parameters that need to be defined as a part of those hooks. These docs are being written in api.php files, in accordance with the standard set forth in Drupal 7. Currently the following files have been created:

Subscribe with RSS Subscribe to RSS - services 3.x