1

I have created a custom service where I am passing parameters and retrieving result from another file.

So to test it I tried on Postman and I put URL with sitename/endpoint/resource and passed parameters in key value. When I submit I get an error as access denied user anonymous.

I have return my custom services under hook_services_resources() function

function mymodule_services_resources() {
 $api = array(
  'blog' => array(
    'actions' => array(
     'math' => array(
      'help' => 'Retrieves posted blogs',
      'file' => array('type' => 'inc', 'module' => 'MYMODULE', 'name' => 'mymodule'),

      'callback' => 'mymodule_blog_retrieve',
      'access arguments' => array('services_access_menu'),

      'access arguments append' => TRUE,
      'args' => array(
        array(
          'name' => 'var1',
          'type' => 'string',
          'description' => 'variable 1',
          'source' => array('data' => 'var1'),
          'default' => '0',
        ),
        array(
          'name' => 'var2',
          'type' => 'string',
          'description' => 'variable 2',
          'source' => array('data' => 'var2'),
          'default' => '0',
        ),
      ),
    ),
  ),
),

 );
  return $api;
}

and inc file just takes parameters var1 and var2 and adds it and returns the sum.

Please help me finding out where i have done mistakes.

2 Answers 2

2

Two suggestions on that.

  1. Try removing 'access arguments append' => TRUE and clear cache. It can interfere with the user_access check.

  2. Depending on your authentication method, you may need to set the correct X-CSRF-Token authentication token in the request. See Problems with Services and CSRF token

1
  • i was working on User View type so had access permissions set at views level not just service module level Commented Feb 3, 2016 at 15:38
3

Here is running code with access callback function:


function mymodule_services_resources() {
 $api = array(
  'blog' => array(
    'actions' => array(
     'math' => array(
      'help' => 'Retrieves posted blogs',
      'file' => array('type' => 'inc', 'module' => 'MYMODULE', 'name' => 'mymodule'),
      'callback' => 'mymodule_blog_retrieve',
      'access arguments' => array('views'),
      'access callback' => '_blog_access_provide_access', 
      'access arguments append' => TRUE,
      'args' => array(
        array(
          'name' => 'var1',
          'type' => 'string',
          'description' => 'variable 1',
          'source' => array('data' => 'var1'),
          'default' => '0',
        ),
        array(
          'name' => 'var2',
          'type' => 'string',
          'description' => 'variable 2',
          'source' => array('data' => 'var2'),
          'default' => '0',
        ),
      ),
    ),
  ),
),

 );
  return $api;
}

/** * Access callback */
function _blog_access_provide_access() {
  return TRUE;
}

I hope this would help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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