Mautic Community Forums

Cannot access Oauth class from ApiAuth

Hi there,

I am building my own extension which should send newly entered e-mail directly to Mautic. I am targeting that to be a Wordpress plugin (I know one exists, I am trying to build slightly different one)



I started by reading this:

https://www.mautic.org/blog/how-to-use-the-mautic-rest-api/



From that, I downloaded the API as ZIP file and included “lib” subdirectory to my directory plugin.



Now, when trying to authorise with Mautic, I start like this:

Code:
function send_to_mautic_after_payment( $payment_id, $payment_data ){ require(plugin_dir_path(__FILE__) . 'lib/MauticApi.php'); require(plugin_dir_path(__FILE__) . 'lib/Auth/ApiAuth.php'); global $global_options;

$edd_mautic_settings = array(
‘baseUrl’ => $global_options[‘mautic_base_url’],
‘clientKey’ => $global_options[‘mautic_public_key’],
‘clientSecret’ => $global_options[‘mautic_secret_key’],
‘callback’ => get_base_url(),
‘version’ => ‘OAuth1a’
);

$auth = MauticAuthApiAuth::initiate($edd_mautic_settings);
if ($auth->validateAccessToken()) {
if ($auth->accessTokenUpdated()) {
$accessTokenData = $auth->getAccessTokenData();
// @todo Save $accessTokenData
// @todo Display success authorization message
} else {
// @todo Display info message that this app is already authorized.
}
} else {
// @todo Display info message that the token is not valid.
}
}


Now, I know that code is pretty basic skeleton, but even running this, I am getting this error:
Code:
Fatal error: Class 'MauticAuthOAuth' not found in /wp-content/plugins/paymenttomautic/lib/Auth/ApiAuth.php on line 45

The 'ApiAuth.php' itself is something provided by the API zip file itself. The code around line 45 says this:
Code:
public function newAuth($parameters = array(), $authMethod = 'OAuth') { $class = 'Mautic\Auth\'.$authMethod; $authObject = new $class(); //... and continued...

Seems to me that this piece code is trying to access the 'OAuth.php' file located in same directory, but is unable to.

I am pretty much clueless what should I do next in order to unblock this and see if I am getting correctly to mautic API. Thanks for help

Hi there,
I am building my own extension which should send newly entered e-mail directly to Mautic. I am targeting that to be a Wordpress plugin (I know one exists, I am trying to build slightly different one)

I started by reading this:
https://www.mautic.org/blog/how-to-use-the-mautic-rest-api/

From that, I downloaded the API as ZIP file and included “lib” subdirectory to my directory plugin.

Now, when trying to authorise with Mautic, I start like this:

function send_to_mautic_after_payment( $payment_id, $payment_data ){
 require(plugin_dir_path(__FILE__) . 'lib/MauticApi.php');
 require(plugin_dir_path(__FILE__) . 'lib/Auth/ApiAuth.php');
 global $global_options;

$edd_mautic_settings = array(
    'baseUrl'           => $global_options['mautic_base_url'],
    'clientKey'         => $global_options['mautic_public_key'],
    'clientSecret'      => $global_options['mautic_secret_key'],
    'callback'          => get_base_url(), 
    'version'           => 'OAuth1a'
);   

$auth = MauticAuthApiAuth::initiate($edd_mautic_settings);
 if ($auth->validateAccessToken()) {
    if ($auth->accessTokenUpdated()) {
        $accessTokenData = $auth->getAccessTokenData();
        // @todo Save $accessTokenData
        // @todo Display success authorization message
    } else {
        // @todo Display info message that this app is already authorized.
    }
} else {
    // @todo Display info message that the token is not valid.
}
}

Now, I know that code is pretty basic skeleton, but even running this, I am getting this error:

 Fatal error: Class 'MauticAuthOAuth' not found in /wp-content/plugins/paymenttomautic/lib/Auth/ApiAuth.php on line 45 

The ‘ApiAuth.php’ itself is something provided by the API zip file itself. The code around line 45 says this:

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

Seems to me that this piece code is trying to access the ‘OAuth.php’ file located in same directory, but is unable to.

I am pretty much clueless what should I do next in order to unblock this and see if I am getting correctly to mautic API. Thanks for help

Ok, I am one more step ahead, but sill far far away from any success.
I have added

require('OAuth.php');

to the function I mentioned above - and to few more places. As long as I hate playing around with API, it seems to be getting to the Mautic.

Now step two: Mautic just tells me this:

Callback is not valid.

I even tried to update the code above and hardcode my callback URL to the script, but still no good
Any ideas are still appreciated

Thank you uncle google for finding this thread:
https://www.mautic.org/community/index.php/525-joomla-api-callback-is-not-valid/0

Now it seems like I need to programatically save the authorisation token :slight_smile: