I been working a PhoneGap (Android, iPhone) based mobile application. PhoneGap uses normal JavaScript so here a list of javascript libraries and where to get them you will need for the XMLRPC interface. I just hope this helps someone else it took me days to track all of this down and get it working.
Here some sample code for calling drupal with the XMLRPC interface:
drupalSession=new drupalSessionObj(); var request = new XmlRpcRequest("http://localhost/?q=services/xmlrpc","system.connect");var response = request.send(); var xmlData=response.parseXML(); if(xmlData.faultCode ){ alert(XMLRPC_ERROR_MSG); } var xmlData=response.parseXML(); //convert to nice javascript object console.debug("d user"+xmlData.user.roles["2"]); drupalSession.sessid=xmlData.sessid; drupalSession.uid=xmlData.user.uid;
Here code to sign a session using API keys
<
pre>
var drupalSessionObj = function(){
this.sessid=null;
this.userName=null;
this.hashStr=null;
this.timeStampStr=null;
this.nounceStr=null;
this.isLoggedIn=false;
this.uid=0;
this.pwd=null;
this.toXml=function(){
return "";
}
this.getUserName=function(){
if(this.userName){
return this.userName;
}else{
return "unknown"+"uid="+this.uid;
}
}
}
var DRUPAL_DOMAIN="localhost"; //the domain your APIKEY is set with
var DRUPAL_APIKEY="generated api key"; //the API key you are using
function getNounce(){
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 12;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
function getUNIXTime(){
var date = new Date();
var formattedTime=""+Math.round((new Date()).getTime() / 1000);
return formattedTime;
}
function signSession(drupalSession,command){
drupalSession.nounceStr=getNounce();
drupalSession.timeStampStr=getUNIXTime();
var strToHash = drupalSession.timeStampStr+";"+DRUPAL_DOMAIN+";"+drupalSession.nounceStr+";"+command;
console.debug("StrToHASH="+strToHash);
digestHexStr = HMAC_SHA256_MAC(DRUPAL_APIKEY,strToHash);
drupalSession.hashStr=digestHexStr;
console.info("signED Session="+drupalSession.toXml()+" cmd="+command );
return drupalSession;
}