3

For an extension I need to create an api user programmatically using php. I use the following code:

 $apiKey = '12345678';
 $user = Mage::getModel('api/user');
    $user->setData(array(
    'username' => 'api_consumer',
    'firstname' => 'fist',
    'lastname' => 'last',
    'email' => 'magento-apiconsumer@mail.com',
    'new_api_key' => $apiKey,
    'api_key_confirmation' => $apiKey,
    'is_active' => 1,
    'user_roles' => '',
    'assigned_user_role' => '',
    'role_name' => '',
    'roles' => array($role->getId())
    ));
    $user->save()->load($user->getId());

When I look in the database the api_key field is not filled, it has the value 'NULL'.

Is it possible to persist the api key?

I madfe a casing mistake, above code works, but I changed

'new_api_key' => $apiKey,

to

'api_key' => $apiKey,
Henry
  • 33
  • 3

1 Answers1

3

I just tested your script and it works. The key is saved in the database. Anyway, if that doesn't work for you try adding this in the setData method:

'api_key' => $apiKey,

Instead of

'new_api_key' => $apiKey,
'api_key_confirmation' => $apiKey,
Marius
  • 197,939
  • 53
  • 422
  • 830
  • Aarrghh, I made a very dumb mistake, a casing issue, I used: $apikey and $apiKey. Thanks for helping! – Henry May 02 '13 at 08:55