Mautic API OAuth

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:

Code:
session_name("oauthtester"); session_start();

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);
}

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:

[code]session_name(“oauthtester”);
session_start();

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);
}[/code]

OK even if I make a call to $auth->getAccessTokenData(); and store the results before the call to accessTokenUpdated, I get token_rejected [code] => 401 when I call $leadApi->getList(). So I’m doing something wrong.

Any ideas?

Thanks.

Good call, although it didn’t fix the issue :frowning:

Hi, this part is totally wrong:

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
  $settings['accessToken'] = $_GET['oauth_token'];
  $settings['accessTokenSecret'] = $_GET['oauth_verifier'];
}

The only way to obtain token is through $auth->getAccessTokenData();

Surprisingly, I started getting the token when I used session_start()

Steps to do next:

  1. Make sure the SESSION is running
  2. Dump the Session out to see what you have stored here
  3. Try the packed API tester which is part of the API library to see if you can access Mautic through there