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

RESTful Web Services

RESTWS exposes Drupal resources (e.g. entities) as RESTful web services. The module makes use of the Entity API and the information about entity properties (provided via hook_entity_property_info()) to provide resource representations.

It aims to be fully compliant to the REST principles.

Installing the Module

  • Copy the whole restws directory to your modules directory (e.g. DRUPAL_ROOT/sites/all/modules) and activate the RESTful Web Services module.
  • Use Drush commands: drush dl restws & drush en restws
  • There is no user interface to configure.

Usage / Testing

To obtain the JSON representation of an entity use your browser to visit: http://example.com/<entity type name>/<entity id>.json
For example: http://example.com/node/1.json or http://example.com/user/1.json would resolve as a JSON dump to client. There are no "service endpoints" to configure as resources are just available at uniform paths like "node/1", "user/1". One can use a client in the form of a browser plugin to test out the different CRUD functions.

Permissions

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.

REST API examples and documentation

Overview

The REST API provided by Services is hard to understand if you've never used Services before and don't understand what are the right URLs to call, what parameters to pass or how to pass them.

There's an effort to document the Services REST API just like you'd expect it documented in any big site, like Twitter or Facebook, it includes the right URL, parameters, how a response would look and more.

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.'),

Pages

Subscribe with RSS Subscribe to RSS - rest