4

I am creating a titanium mobile app that consumes and uses Drupal Services. I'm able to create a node but the user is always anonymous. I've tried setting the request headers so that the Authentication is basic.

authstr = 'Basic ' +Titanium.Utils.base64encode('admin'+':'+'password'); 
xhr.setRequestHeader('Authorization', authstr);  
xhr.setRequestHeader("Content-Type","application/json");
xhr.send(JSON.stringify(params));

For some reasons it still gives me;

Code=3 "Authentication needed"

What is the best way to troubleshoot this? Is it even possible to use sessions in this manner?

3 Answers 3

7

Session authentication is different than Basic authentication.

Session authentication requires that you login, by sending a POST request to ENDPOINT/user/login. It will return the session header information and you need to store that for future use and send it along with every request.

3
  • ok, got it, I'm abble to retried the session ID in the mannor, but how do I pass this along with subsequent requests? so I set the session ID as a request header, or pass it in the parameters of the JSON string.
    – user379468
    Nov 16, 2011 at 21:13
  • 1
    You pass it as a header. Nov 16, 2011 at 21:44
  • What is the appropriate header type to pass it along in subsequent requests? Jun 16, 2012 at 11:01
9

Thank you very much for your help. I finally manage to autenticate and retrieve files from my rpc server. Here is my syntax in case anyone needs it.

// USER LOGIN
$authenticate = xmlrpc($settings['server'], Array('user.login' => array($settings['user'] , $settings['password']),));

// CREATE HEADERS
$options['headers']['Cookie'] = $authenticate['session_name'] . '=' . $authenticate['sessid'];

//RETRIEVE FILE
$file= xmlrpc($settings['server'], Array('file.retrieve' => Array(Array("fid"=>$fid),Array("file_contents"=> 'true'),Array("image_styles"=>'true') )),$options);

EDIT: AFTER SERVICES 3.5 you should do:

// USER LOGIN
$authenticate = xmlrpc($settings['server'], Array('user.login' => array($settings['user'] , $settings['password']),));

// CREATE HEADERS
$options['headers']['Cookie'] = $authenticate['session_name'] . '=' . $authenticate['sessid'];

// GET TOKEN
$token = xmlrpc(url($this->endpoint, array('absolute' => TRUE)), array('user.token' => array()), $this->headers);

//INSERT TOKEN INTO HEADERS
$options['headers']['X-CSRF-Token'] = $token['token'];

//RETRIEVE FILE
$file= xmlrpc($settings['server'], Array('file.retrieve' => Array(Array("fid"=>$fid),Array("file_contents"=> 'true'),Array("image_styles"=>'true') )),$options);
2
  • $settings['server'] is Drupal REST API Server or XMLRPC Server?
    – AshwinP
    Nov 25, 2013 at 12:57
  • Hi AshwinP, it's the url of the XMLRPC server endpoint, something like acme.com/services/rpc. (I think REST authentication should be similar, but I never tried it) Dec 17, 2013 at 10:22
0

you can use services_basic_auth module (https://drupal.org/project/services_basic_auth) to enable basic authentication for drupal services module .

After installing services_basic_auth module you need to enable it by going to, Structure-->Services-->Edit

enter image description here

Now you can use Basic Authorization Header to create nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.