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

1. Add a service endpoint ( You may use an existing endpoint and skip to step 2 )
- Structure ➔ Services
- ✚Add
- Enter the name ( this is the first part of the URL to this service resource )
- Select REST under Server
- Enter the path, preferably the same as the name
- Save

2. Add a service view (Representing a single REST resource)
- Structure ➔ Views
- Add new view; check ONLY Create a block; enter name; continue & edit
- ✚Add ➔ Services ( if there is no Services, the services view module is not installed/enabled )
- FILTERS ➔ add; choose the content type to be included in the REST output
- Click / under SERVICE SETTINGS; enter the path for this view ( this is the second part of the URL to this service resource )
- Configure other options as needed
- Save

3. Configure the service endpoint
- Structure ➔ Services
- Edit Resources under OPERATIONS
- Check your service view name
- Save
- SERVER Tab ( you are currently in the RESOURCES tab )
- Choose your Response formats ( Checks only "json" if you want an JSON array )
- Save

Config OAuth

1. Enabling the modules
- Open modules
- Check OAuth under OAUTH
- Check OAuth Provider UI under OAUTH
- Check OAuth Authentication under SERVICES - AUTHENTICATION
- Configuration ➔ OAuth under WEB SERVICES; make sure Enable the oauth provider is checked

2. Add an OAuth Context
- Configuration ➔ OAuth ➔ ADD CONTEXT tab.
- Under SIGNATURE METHODS check HMAC-SHA1 and uncheck all the others
- Under AUTHORIZATION LEVELS enter name and title, preferably the same for both. Check Selected by default
- Save

3. Modifying the endpoint
- Structure ➔ Services
- Under OPERATIONS of the target endpoint select Edit
- Check OAuth authentication
- Save
- Under OPERATIONS of the target endpoint select Edit Authentication
- Under OAuth context select the name of your context
- Under Default required authentication select Consumer key, also known as 2-legged OAuth
- Save
- Select your defined level under Default required OAuth Authorization level
- Save

4. Add an OAuth Consumer (Representation of your external application within OAuth)
- People ➔ ✚ Add user
- Enter a name; for example: "OAuth User"; e-mail and password are not used, so enter anything
- Save
- View your new user's profile by clicking on its name
- Authorization ➔ Add consumer
- Name your consumer something related to your external application that will use the REST resource. For example: frontpage_news
- The callback URL is not used. Select your context. Save

5. Update Drupal Permission
- People ➔ Permissions tab; scroll to OAuth
- Check all boxes for Register OAuth consumers in [your context name] and Authorize OAuth consumers in [your context name]
- Save

Given the correct setup, your resource is available at the following URL:

you Drupal site base URL / path to an endpoint / path to a view

for example: library.utoronto.ca/rest/catalogue

PHP

Installation

pecl and phpize are required for OAuth. Open a terminal and type pecl and phpize to test if they are installed.

wget http://pear.php.net/go-pear.phar
 php go-pear.phar
 apt-get install php5-dev
 pecl install phpize
 pecl install oauth

Afterward, you must add to php.ini this line extension=oauth.so
In php.ini, search for "extension" and paste the line there.
If you have more than one php.ini, add to all of them.

Installation for Ubuntu 14.04

To install the PHP oauth extension for Ubuntu 14.04, the following will suffice:

sudo apt-get install php5-oauth

Usage

Define a constant for your resource at the top of your PHP file:

 define("EFEED", 'http://mydrupal.utoronto.ca/some_endpoint/some_view');

Get consumer key and secret from Drupal
- People ➔ [Your OAuth User] ➔ Authorization ➔ Consumers ➔ Edit under your external application's Consumer
- Copy the secret and key into PHP

 $consumer_key = '8NMxrPkxdU8GAaLvsaP5qDKeLdVfRvWr';
 $consumer_secret = '2G79cLpxsH4tuL2Mtv6G9h9QURGjMb3R';

Instantiate new OAuth object

$oauth = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();

Fetch the REST resource

$data = $oauth->fetch(EFEED);
$response_info = $oauth->getLastResponse();
$array = json_decode($response_info);

Parse the JSON array

if ($array) {		
  $object = $array[0]; // The array could contain multiple instances of your content type
  $title = $object->title; // title is a field of your content type
}

Happy Drupaling!

Comments

alex_chew’s picture

Hi,

This is a great article. I configured the server successfully by following those steps. And it works when I using an OAuth PECL implementation as client. But I kept getting "HTTP_OAuth_Consumer_Exception_InvalidResponse:Failed getting token and token secret from response" exception while I changed to client to HTTP_OAuth 0.2.3 Pear implementation. Could you please show me some tips on this problem? Many thanks.

Best Regards,
Alex

rosso69’s picture

I try to create a rest server on a debian squeeze server. Using all to modules you refer to but it won't work.

Do you know if the drupal core has changes which can affect this?

Event Horizon’s picture

Thanks for documenting this. It's brilliant and has saved me hours of work.

Dharmendra.s’s picture

great article it works fine , but now i want to POST data on remote site, how can i post the data can you please suggest me,

like i want to create node then we need to pass the other parametes also , i have stuck on this
i am using drupal 7

goldmantx’s picture

Thanks for the instructions! Worked like a champ!

catherina’s picture

I configutation and only choose the json,and show the node informartion on rest/node/nid.I want to add the node‘s editor all information in this json,how can i do?

hasjalil’s picture

Very clear

selva8187’s picture

This tutorial gives clear information