Hi,

I am trying to login the user programmatically. And that was working good and I am getting session details and csrf token. But the csrf token I got is not valid I can't use the csrf token for another tasks ie logout. It's throwing csrf validation error.
The below code is I am using for login and getting token.

function login_user($user_object){
  

  $form_state['uid'] = $user_object->uid;
  user_login_submit(array(), $form_state);

$sessionid = session_id();
  $session_name = session_name();
  $token = drupal_get_token(); //this token is not valid. How to get correct token?

  return $token;
}

Comments

ssuresh1295 created an issue. See original summary.

ssuresh1295’s picture

Issue summary: View changes
tyler.frankenstein’s picture

Category: Task » Support request
Priority: Critical » Normal

Are you trying to login from JavaScript?

ssuresh1295’s picture

Category: Support request » Task
Priority: Normal » Critical

I am trying to login from android mobile using drupal rest services.

tyler.frankenstein’s picture

Category: Task » Support request
Priority: Critical » Normal

Please don't adjust the category and priority, this is a normal support request, thanks.

I myself am not familiar with using PHP to login to Services, I'm only familiar with using JavaScript to login to Services.

That being said, I seem to recall it being necessary to pass along the session id and session name concatenated onto each other (with some type of separator) and that string is then sent along as a header (and I'm not sure what the key on that should be). That should set you in the right direction, there should be some examples out there, perhaps on drupal.stackexchange.com.

Other than that, perhaps others will chime in with advise, good luck and happy coding.

ssuresh1295’s picture

I found the issue. We should not get token using drupal_get_token();. We should pass services string to that function drupal_get_token('services'); like this. Thanks.

tyler.frankenstein’s picture

Status: Active » Closed (works as designed)

Excellent. Thank you for confirming.