Hi,
I’m a little confused regarding the OAuth authentication, specifically the call $auth->accessTokenUpdated(). It never seems to return true. As a result I have make a call to $auth->getAccessTokenData(); and store the results before the call to accessTokenUpdated. Am i missing something? Thanks
My sample code based on sample from https://github.com/mautic/api-library:
require dirname(DIR).’/vendor/autoload.php’;
require ‘settings.php’;
use MauticAuthApiAuth;
use MauticMauticApi;
// ApiAuth::initiate will accept an array of OAuth settings
$settings = array(
‘baseUrl’ => $baseUrl, // Base URL of the Mautic instance
‘version’ => $version, // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
‘clientKey’ => $clientKey, // Client/Consumer key from Mautic
‘clientSecret’ => $clientSecret, // Client/Consumer secret key from Mautic
‘callback’ => $callback // Redirect URI/Callback URI for this script
);
if (isset($_GET[‘oauth_token’]) && isset($_GET[‘oauth_verifier’])) {
$settings[‘accessToken’] = $_GET[‘oauth_token’];
$settings[‘accessTokenSecret’] = $_GET[‘oauth_verifier’];
}
// Initiate the auth object
$auth = ApiAuth::initiate($settings);
if (isset($_SESSION[‘accessTokenData’])) { //todo read from more permanent
$auth->setAccessTokenDetails(json_decode($_SESSION[‘accessTokenData’], true));
}
if ($auth->validateAccessToken()){
echo ‘222
’;
$accessTokenData = $auth->getAccessTokenData();
$_SESSION[‘accessTokenData’] = json_encode($accessTokenData); //todo save more permanently
if ($auth->accessTokenUpdated()) {
echo ‘333
’;
$accessTokenData = $auth->getAccessTokenData();
//store access token data however you want
}
$leadApi = MauticApi::getContext(“leads”, $auth, $baseUrl .’/api/’);
$leads = $leadApi->getList();
echo ‘$leads =’ . print_r($leads, true);
}