Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login remotely to Drupal 7 site using services module

GOAL:

I want my web app to be able to send username and password of a user on my Drupal site, to the Drupal site and have the Drupal site respond with confirmation of whether the user authentication information is correct.

Another need is to have the Drupal server return a session id for a session created, and information for the user, like email, user role, etc.

What I have so far:

I've enabled Services Rest Server, and created an endpoint at www.mysite.com/rest/. I've set the response format to json only, and request parser to application/x-www-form-urlencoded. I've turned on authentication by session, debugging mode. I've also made sure to enable the entire user resource for this endpoint. Navigating to www.mysite.com/rest/user does give me a list of all users.

Regarding my attempts with authentication, I've tried sending requests:

POST www.mysite.com/rest/user/login
    Content-Type: application/x-www-form-urlencoded
    POST Body:
        name:admin,
        pass:myadminpassword

But it just keeps returning a 401 Unauthorized response. I've double checked and the auth information is correct. I've tried other variations on parameter names like: (username, password), (user, pass), (name, password), etc.

I can't seem to get the login to work and I've looked through other examples I've found on Stackoverflow but they haven't worked.

like image 515
user594044 Avatar asked Aug 14 '12 23:08

user594044


1 Answers

There is good documentation for the services module in drupal.org. Particulary there are several examples for using servicers here: http://drupal.org/node/783722

The example on this page is using command line tool curl: http://drupal.org/node/1795770

And contains one example for login:

curl -X POST -i -H "Content-type: application/json" -c cookies.txt -X POST http://localhost:8888/service/user/login -d '
{
    "username":"user",
    "password":"password"
}
'

So:

  • you have your endpoint ok
  • Content-type should be application/json
  • and the body should be something like { "username":"admin", "password":"pass" }
like image 156
jackbravo Avatar answered Nov 06 '22 04:11

jackbravo