Calling Services using JavaScript

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
kscottj's picture

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.

  • XMLRPC (http://mimic-xmlrpc.sourceforge.net/)
  • sha HMAC (http://point-at-infinity.org/jssha256/ needed if you want to use API keys)

    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;
    

    }

  • Services

    Group organizers

    Group categories

    Group notifications

    This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

    Hot content this week