Log in remotely to the Drupal 7 site using the service module

TASK:

I want my web application to be able to send the username and password of the user on my Drupal website, to the Drupal website and respond to the Drupal website confirming that the user authentication information is correct.

Another need is for the Drupal server to return the session identifier for the created session and information for the user, such as email, user role, etc.

What I still have:

I turned on the Rest Rest Services server and created the endpoint at www.mysite.com/rest/. I set the response format for json only and requested a parser for the / x -www-form-urlencoded application. I turned on session authentication, debug mode. I have also included the entire user resource for this endpoint. Going to www.mysite.com/rest/user gives me a list of all users.

As for my authentication attempts, I tried to send requests:

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

But it just returns 401 Unauthorized response. I checked twice and the authentication information is correct. I tried other options for parameter names, such as: (username, password), (user, password), (username, password), etc.

I can’t get the login to work, and I looked at other examples that I found in Stackoverflow, but they didn’t work.

+7
source share
1 answer

There is good documentation for the service module at drupal.org. In particular, there are several examples of the use of services here: http://drupal.org/node/783722

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

And it contains one example for logging in:

 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 endpoint ok
  • Content-type must be application / json
  • and the body should be something like {"username": "admin", "password": "pass"}
+4
source

All Articles