Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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);

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a drupal.stackexchange
scroll top