Community Documentation

Example: Accessing a service from Groovy or Java

Last updated April 6, 2010. Created by reikiman on March 31, 2007.
Edited by heyrocker, asogor, bekasu. Log in to edit this page.

The Groovy language bundles XMLRPC service in the download. That, and the fact Groovy is a dynamic scripting language, makes it an interesting language to use for XMLRPC applications.

This example uses the echo service elsewhere in this handbook.

On the serverProxy line enter the correct URL.

Go into the Administration >> Services section and click on the Keys tab. Click on Create Key, enter anything for the Application Title and leave the Domain blank. Then paste the value you're given into the value for apiKey.

Then for userName and password enter appropriate values.

In the line saying serverProxy.echo.echo enter any message you want to have printed.

Run the script using the command line: groovy echo.groovy

import groovy.net.xmlrpc.*
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Mac;
import java.nio.charset.Charset;
import java.nio.ByteBuffer;

def host = "localhost:1080";
def serverProxy = new XMLRPCServerProxy("http://" + host + "/cms/services/xmlrpc")
def csets = Charset.forName("US-ASCII");
def sharedkey = "<your API key>";
def nonce = ""+System.currentTimeMillis();
def timestamp = System.currentTimeMillis();
def keySpec = new javax.crypto.spec.SecretKeySpec(csets.encode(sharedkey).array(), "HmacSHA256");
def mac = javax.crypto.Mac.getInstance("HmacSHA256");
mac.init(keySpec);

def hash = mac.doFinal(csets.encode(timestamp+";"+host+";"+nonce+";"+"user.login").array());

def result = "";
for (int i=0; i < hash.length; i++) {
    result += Integer.toString( ( hash[i] & 0xff ) + 0x100, 16).substring( 1 );
}
println result;
def loginData = serverProxy.user.login(result,"localhost:1080",""+timestamp,nonce,serverProxy.system.connect()["sessid"],"gamemaker","openup");
sessionId = loginData["sessid"];
println loginData
println sessionId                                                                                                            
println serverProxy.node.get(sessionId,5,[]);

A java xmlrpc client example is available here

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 6.x

Develop for Drupal

Drupal’s online documentation is © 2000-2012 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.
nobody click here