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 encryption
obj.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.get
obj.view_name = 'the_drupal_view';

// create the connection to our services module and send json data via POST
var xhr = Titanium.Network.createHTTPClient();
xhr.open("POST", servicesURL);
xhr.send({data: JSON.stringify(obj)});

// once our data has returned the onload function is used
xhr.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值