Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to call an undefined method named "validateAccessToken" when using Mautic api-library in Symfony

I'm trying to use mautic/api-library inside my Symfony project. I'm using Symfony 2.8.9 with PHP 5.6.14.

I've included api-library project in composer and in the autoload.php file. In my controller, I've declared api-library classes:

use Mautic\Auth\ApiAuth;
use Mautic\Auth\OAuth;

And tried to get a token from my mautic installation:

$settings = array(
    'baseUrl'      => 'http://mymauticinstallation.com',
    'version'      => 'OAuth1a',
    'clientKey'    => 'myCLientKey',    
    'clientSecret' => 'mySecretClient',  
    'callback'     => 'https://api.mysymfonyapp.com/'
);
$auth = new ApiAuth();
$auth->newAuth($settings);
if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
    }
}

But when I try to run this code I'm getting this error in my console:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "validateAccessToken" of class "Mautic\Auth\ApiAuth"

Looking inside mautic ApiAuth class, newAuth method uses a instantiation by refection:

public function newAuth($parameters = array(), $authMethod = 'OAuth')
{
    $class      = 'Mautic\\Auth\\'.$authMethod;
    $authObject = new $class();

    ...

    return $authObject;
}

According to the exception message, the reflection is not returning an OAuth class instance. Do anyone know what is causing this? I've checked and I'm meeting minimum requirements for PHP and Symfony. Is there anything related to PHP version and reflection?

Thanks in advance.

like image 408
Hugo Nogueira Avatar asked Aug 26 '16 02:08

Hugo Nogueira


1 Answers

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "validateAccessToken" of class "Mautic\Auth\ApiAuth"

Means the method validateAccessToken doesn't exist in Mautic\Auth\ApiAuth, indeed it's not defined there but in Mautic\Auth\OAuth.

// Mautic\Auth\ApiAuth
public function newAuth($parameters = array(), $authMethod = 'OAuth')
{
    $class      = 'Mautic\\Auth\\'.$authMethod;
    $authObject = new $class();

    ...

    return $authObject; // <-- it returns an object, use it!
}

So what you missed is to store the returned object in a variable to use it

$apiAuth = new ApiAuth();
$auth = $apiAuth->newAuth($settings);
if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
    }
}
like image 187
lolmx Avatar answered Sep 28 '22 08:09

lolmx