开发者

How can I retrieve a user using a query string from drupal using REST?

What I'd like

I want to be able to reset a users password without emailing them. I also need to do thi开发者_StackOverflows via REST (it's for a kiosk system).

So I need to:

  1. Get user with a specific username
  2. Reset the password of that user

Try as I might, I can't get the user with a username.

The problem

After I've logged in using the admin user via REST I do:

GET on http://mydomain.com/rest/user?name=user.

According to REST documentation, this should get the user with name user.

I get a 200 response, but nothing is returned in the body.

What I've Tried

Get node from ID

Once I've logged in as admin, I can do:

GET on http://mydomain.com/rest/user/1

This works, returning the details of user 1 in the response. But I need to search for a user by username.

Different GETs

GET on http://mydomain.com/rest/user/admin
GET on http://mydomain.com/rest/user/account/name/admin
GET on http://mydomain.com/rest/user?account[name]=admin
GET on http://mydomain.com/rest/user?uid=1
GET on http://mydomain.com/rest/user/retrieve?uid=1
GET on http://mydomain.com/rest/user?account[uid]=1

All these fail.

Nodes instead of users

GET on http://mydomain.com/rest/node/1 works, but http://mydomain.com/rest/node?nid=1 gives me a 200 response with nothing in the body.

List of all users

GET on http://mydomain.com/rest/user/ doesn't show a list of all users either. I get a 200 with empty body again.

Further Details

  • I'm working with a Drupal 6.22 install.
  • I've installed the services module (3.0 RC1) and REST Server (2.0 beta 1).
  • I've got the session authentication module installed.
  • I've tried the above with the session authentication switched on and with it off.
  • I've enabled all node resources and user resources for the REST endpoint I've set up.
  • I've tried the above with and without clean URLs and nothing seems to make a difference.

Question

How do I get a user with a specific username? What am I doing wrong?

Update

I've uninstalled then reinstalled Services module, I've done the same with the REST server. I've also rolled back my version of CTools to the latest recommended one. I've added a brand new service in case it was corrupted. Nothing I do works. Frustrating!

Update 2

I've found the problem, but would like a more permanent solution. It turns out it was an error because of table naming. See my current accepted answer.


Im sorry this is so frustrating for you. Im the maintainer for services module and were really working on the documentation but I thought I would answer here to just get you moving along.

The reason you are not getting any of the data you want is because the parameters you are passing are wrong.

If you look at user_resource.inc index is defined as follows

      'index' => array(
    'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/user_resource'),
    'callback' => '_user_resource_index',
    'args' => array(
      array(
        'name' => 'page',
        'optional' => TRUE,
        'type' => 'int',
        'description' => 'The zero-based index of the page to get, defaults to 0.',
        'default value' => 0,
        'source' => array('param' => 'page'),
      ),
      array(
        'name' => 'fields',
        'optional' => TRUE,
        'type' => 'string',
        'description' => 'The fields to get.',
        'default value' => '*',
        'source' => array('param' => 'fields'),
      ),
      array(
        'name' => 'parameters',
        'optional' => TRUE,
        'type' => 'array',
        'description' => 'Parameters',
        'default value' => array(),
        'source' => array('param' => 'parameters'),
      ),
    ),
    'access arguments' => array('access user profiles'),
    'access arguments append' => FALSE,
  ),

Notice the third argument, parameters. You can do all sorts of fun stuff with the index query as long as it is turned on, but what you havnt tried is ?parameters[name]=user Example below

When I make a request to http://test/api/user?parameters[name]=gdsfgsdfgsdfg&fields=name,uid I get returned

[
 {
  "name":"gdsfgsdfgsdfg",
  "uid":"36",
  "uri":"http:\/\/test\/api\/user\/36"
 }
]

Notice i also added some fields, uid and name. Obviously these are optional but it shows you the power of the index query. The same applies to nodes.

I hope this helps.

How can I retrieve a user using a query string from drupal using REST?


Try something like:

GET on http://mydomain.com/rest/user?parameters[name]=admin

This worked for me in Drupal7. Not sure if it'll be the same for 6. But I also had no issues with getting the index of all users using GET on http://mydomain.com/rest/user/


I found the issue. It's an error/bug with the Services module in the setup I have.

From the services.module file

449  // Now implode that array into an actual WHERE clause.
450  $where = !empty($where) ? ' WHERE '. implode(' AND ', $where) : '';

451  // Run through db_rewrite_sql to make sure proper access checks are applied.
    // **** Error logged for this next line ****
452  $sql = "SELECT $fields FROM {$table} $where ORDER BY $order"; // <-- Error was logged for this line
453  $sql = db_rewrite_sql($sql);
454  $result = db_query_range($sql, $parameters, $page * 20, 20);
455  return $result;

The error

  Table '<REDACTED>_drup1.users' doesn't exist query: SELECT * FROM users ORDER BY created DESC LIMIT 0, 20 in /<REDACTED>/sites/all/modules/services/services.module on line 455.

The users table in my installation is called drup_users. When I change line 452 to

452  $sql = "SELECT $fields FROM drup_{$table} $where ORDER BY $order";

It works. I'd be interested in knowing how I can have a more permanent solution. I ran update.php several times and it didn't fix this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜