12

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.

Stephan
  • 41,764
  • 65
  • 238
  • 329
Hugo Nogueira
  • 1,298
  • 1
  • 12
  • 24

1 Answers1

1
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();
    }
}
lolmx
  • 521
  • 6
  • 18
  • you answer makes sense. But it didn't solve the problem. Actually the error message changed, but I'm not sure it's the solution. If you look to mautic/api-library docs (https://github.com/mautic/api-library) it's done the exact way I'm doing, using the $apiAuth by reference. Anyone has any other idea? – Hugo Nogueira Sep 07 '16 at 17:06
  • I'm having a weird error, but I couldn't see the logs yet. I'll create a brand new application with raw api code and post here. Thanks! – Hugo Nogueira Sep 22 '16 at 14:10