Igor Simic
4 years ago

Drupal 7 Services - login using REST API

How to login using Drupal 7 module "Services" via REST?  If you try to access some resources without login in before most likely you will get this error message: "Access denied for user anonymous with X-CSRF-Token and Cookie"

To solve this issue you have to login first. To be able to login, you have to create endpoint with login resource

1. go to  Structure->Services->add entry fill in the fields and select Session authentication under Authenitcation (i named it test_entry)
2. click again on Structure->Services and and select Edit resources for your endpoint
3.  under user option select login, logout  and token (and retrieve if you like)
4. now go to your endpoint using postman yourdomain.com/test_entry/user/token  and send your username and password in json object:
  • {
  • send the headers
  • application/json
postman get token

4. take token from response and go to yourdomain.com/test_entry/user/login and put token in header as X-CSRF-Token
5 in response you will get user object including token, use that token for any future requests as new X-CSRF-Token value

The End