Example: Accessing a service from Groovy or Java
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
