1

I am trying to fetch custom table result anonymously with services but I am getting

<?xml version="1.0" encoding="utf-8"?>

Access denied for user anonymous

My Code are as follow :

    function custom_data_services_resources() {
  return array(
    'getdata' => array(
      'retrieve' => array(
        'help' => 'Retrieves a data',
        'file' => array('file' => 'inc', 'module' => 'custom_data'),
        'callback' => '_get_data',
        'access callback' => TRUE,
        'args' => array(
          array(
            'name' => 'id',
            'type' => 'int',
            'description' => 'The id of the note to get',
            'source' => array('path' => '0'),
            'optional' => FALSE,
          ),
        ),
      ),
      'index' => array(
        'help' => 'Retrieves a listing of notes',
        'file' => array('file' => 'inc', 'module' => 'custom_data'),
        'callback' => '_get_data',
        'access callback' => TRUE,
        'args' => array(
          array(
            'name' => 'page',
            'type' => 'int',
            'description' => '',
            'source' => array(
              'param' => 'page',
            ),
            'optional' => TRUE,
            'default value' => 0,
          ),
          array(
            'name' => 'parameters',
            'type' => 'array',
            'description' => '',
            'source' => 'param',
            'optional' => TRUE,
            'default value' => array(),
          ),
        ),
      ),
    ),
  );
}

I tried with "X-CSRF-Token" also But no luck :(

Any help appreciated

2

Following up with an answer from our comments...

Here's an example custom service resource:

custom_data.module

function custom_data_services_resources() {
  $resources = array(
    'custom_data_resources' => array(
      'actions' => array(
        'getdata' => array(
          'help' => t('Gets custom data.'),
          'file' => array(
            'type' => 'inc',
            'module' => 'custom_data',
            'name' => 'custom_data.resource',
          ),
          'callback' => 'custom_data_getdata',
          'args' => array(
            array(
              'name' => 'data',
              'type' => 'array',
              'description' => 'The data dude.',
              'source' => 'data',
              'optional' => FALSE,
            )
          ),
          'access callback' => 'custom_data_access',
          'access callback file' => array(
            'type' => 'inc',
            'module' => 'custom_data',
            'name' => 'custom_data.resource',
          ),
          'access arguments' => array('access user profiles'),
          'access arguments append' => TRUE,
        ),
      ),
    ),
  );
  return $resources;
}

custom_data.resource.inc

function custom_data_getdata($data) {
  return array('foo' => $data['bar']);
}

function custom_data_access($permission) {
  return user_access($permission);
}

After that, just enable the custom resource under your endpoint's configuration and clear all of Drupal's caches. Then you can make a POST to:

?q=[my-endpoint]/custom_data_resources/getdata.json

If you need to send along custom arguments, I usually recommend a single array parameter, that way you can easily expand upon it and send as many variables/properties you need along with the payload:

{ "bar": 123 }

Which will then return to you:

{ "foo": 123 }

Don't forget to attach a header to your request with a name of X-CSRF-Token and value retrieved from: ?q=services/session/token

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.