How can I create a RESTful role and consumer during the installation of an extension (module) using a data-install script located at:


  • I've achieved this (and will post answer later), but curious to see other possible methods and figure this is a good informative post as I could not find this info. anywhere. – Tim Hallman Jun 3 '15 at 15:06
  • How long do you want to wait? – Fabian Schmengler Jul 15 '15 at 20:31

I was spent many hours searching and finally, I got the solution from Magento core. Magento itself provide code for create rest role programmatically and assign this role to the user. we can also programmatically generate rest oAuth consumer secrete and api key. please see the following code.

try {

         $role = Mage::getModel('api2/acl_global_role')
          $rule = Mage::getModel('api2/acl_global_rule');
          $id = $role->getId();

         //create new user write you firstname,lastname

         $user = Mage::getModel('admin/user')
           'username'  => $username,
           'firstname' => $username,
           'lastname' => $username,
           'email'     => $username.'email@gmail.com',
           'password'  => $password,
           'current_password' => $password,
           'api2_roles'=> array($role->getId()),
           'is_active' => 1

          // Assign Role to user

            $uRoles = array(1);

          //oAuth consumer
                  $helper = Mage::helper('oauth');
                  $model = Mage::getModel('oauth/consumer')
                   'key'  => $helper->generateConsumerKey(),
                   'secret' => $helper->generateConsumerSecret(),
                   'name' => 'restuserapp',
                  'current_password' => $password

                  $insertId = $model->save()->getId();

    } catch (Exception $e) 

     echo $e->getMessage();


This code is work for me. Hope it helps someone.

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.