Belcan
June 27, 2013, 8:56am
1
Hello,
I try to use the API of Piwik to creat new user with the method UsersManager.addUser.
<?php
define('PIWIK_INCLUDE_PATH', realpath('../stats'));
define('PIWIK_USER_PATH', realpath('../stats'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
Piwik_FrontController::getInstance()->init();
$request = new Piwik_API_Request('
method=UsersManager.addUser
&userLogin=test
&password=test
&email=test@gmail.com
&alias=alias
&token_auth=55c770f00c299076bfedc869f7dd6d09
');
$result = $request->process();
echo $result;
?>
But it is not work but when I do a other request like :
$request = new Piwik_API_Request('
method=UsersManager.getUsersAccessFromSite
&idSite=2
&token_auth=55c770f00c299076bfedc869f7dd6d09
');
It’s work and I can see this user have the access “view”.
The token_auth is the token of the main user when I had install piwik and I found it when I go on API in the top menu when I’m log on piwik.
Do you have any idea?
matthieu
(Matthieu Aubry)
June 27, 2013, 9:37am
2
You forgot to call UsersManager.setUserAccess after creating user
Belcan
June 27, 2013, 10:12am
3
Thanks for your reply. Now I have :
<?php
define('PIWIK_INCLUDE_PATH', realpath('../stats'));
define('PIWIK_USER_PATH', realpath('../stats'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
// if you prefer not to include 'index.php', you must also define here PIWIK_DOCUMENT_ROOT
// and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
Piwik_FrontController::getInstance()->init();
$request = new Piwik_API_Request('
method=UsersManager.addUser
&userLogin=test
&password=test
&email=test@gmail.com
&alias=alias
&token_auth=55c770f00c299076bfedc869f7dd6d09
');
$result = $request->process();
echo $result;
$request = new Piwik_API_Request('
method=UsersManager.setUserAccess
&userLogin=test
&access=view
&idSites=2
&token_auth=55c770f00c299076bfedc869f7dd6d09
');
$result = $request->process();
echo $result;
?>
But don’t work I miss something? For information the idSites 2 exist.
After I use the method addUser the new user is not creat in the database.
matthieu
(Matthieu Aubry)
June 30, 2013, 9:31pm
4
replace echo with var_dump
and what is the output your script gives you? if no user is created then the API should return an error
Belcan
July 1, 2013, 10:35am
5
Now I got this :
<?php
define('PIWIK_INCLUDE_PATH', realpath('../stats'));
define('PIWIK_USER_PATH', realpath('../stats'));
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
// if you prefer not to include 'index.php', you must also define here PIWIK_DOCUMENT_ROOT
// and include "libs/upgradephp/upgrade.php" and "core/Loader.php"
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
Piwik_FrontController::getInstance()->init();
$request = new Piwik_API_Request('
method=UsersManager.addUser
&userLogin=test
&password=test
&email=test@gmail.com
&alias=alias
&token_auth=55c770f00c299076bfedc869f7dd6d09
');
$result = $request->process();
var_dump($result);
?>
And I got this result :
string(145) " "
New user not add to the database of piwik.