当前位置: 代码迷 >> Iphone >> Drupal connected iPhone application using services and authentication
  详细解决方案

Drupal connected iPhone application using services and authentication

热度:498   发布时间:2016-04-25 06:12:15.0
Drupal connected iPhone application using services and authentication.

So I’ve built my first Drupal connected iPhone application. It uses the services module and authentication. I couldn’t find any code examples of using authentication online anywhere so I am including some code examples in this post in hopes that it will help someone else.

Firstly, I followed the tutorial I found in the CivicActions blog, and after I saw how to use the code to connect to a web service, I decided to look for how to use authentication, since the website I am building an app for uses authentication for the web services.

I used the suggested JS SHA256 library in the CivicActions tutorial, and came up with the method to use it properly while communicating with the services module.

Here is the code you’ll need, keep in mind this is using Titanium Developer, all code is written in Javascript, and you’ll need the iPhone emulator in order to see the results.

var servicesURL = 'http://example.com/services/json';var domain = 'example.com';var date = new Date();var obj = {  method: service,  domain_name: domain,  domain_time_stamp: date.getTime(),  nonce: rnd(), /* just a random function that returns a random string */  sessid: sessionid};// create the hash using secure hash algorithm using 256bit encryptionobj.hash = HMAC_SHA256_MAC(apikey, obj.domain_time_stamp+";"+obj.domain_name+";"+                 obj.nonce+";"+obj.method);// this is your view name, be sure your authentication key allows access to views.getobj.view_name = 'the_drupal_view';// create the connection to our services module and send json data via POSTvar xhr = Titanium.Network.createHTTPClient();xhr.open("POST", servicesURL);xhr.send({data: JSON.stringify(obj)});// once our data has returned the onload function is usedxhr.onload = function(){  // perform your code here with the data coming back,  // data will be the object containing the response.  var data = JSON.parse(this.responseText);  // you can use Ti.API.info(JSON.stringify(data)) to output your response to the console}

After I figured out how to put together the proper call out to the services from Javascript, I was able to format some data, here is my result:

iPhone application

Drupal connected iPhone application

Download the JS SHA265 library: js sha256 version 0.1

  相关解决方案