Can't log via API

Hi, I can’t log in via API. I am trying to use OAuth2 from https://github.com/mautic/api-library
But it always ends on 401 - API authorization denied. Could anyone help me please? I am trying to solve this problem about 8 hours now, so it’s amazingly frustrating.

PHP 7.1
Mautic v2.12.1 - installed on localhost


<?php

// Bootup the Composer autoloader
include __DIR__ . '/vendor/autoload.php';  

use Mautic\Auth\ApiAuth;

session_start();

$publicKey = '';
$secretKey = '';
$callback  = '';

// ApiAuth->newAuth() will accept an array of Auth settings
$settings = array(
    'baseUrl'          => 'http://mautic.lh',       // Base URL of the Mautic instance
    'version'          => 'OAuth2', // Version of the OAuth can be OAuth2 or OAuth1a. OAuth2 is the default value.
    'clientKey'        => '1_14ky13whdfggsw8oc0ow8oks8ks80cs4oocs0kwco88go4c84c',       // Client/Consumer key from Mautic
    'clientSecret'     => '15kn10uoa6bk8oc08048sg0444c4g04o0gkk008cs40c8oc4ss',       // Client/Consumer secret key from Mautic
    'callback'         => 'http://myproject.lh'        // Redirect URI/Callback URI for this script
);

/*
// If you already have the access token, et al, pass them in as well to prevent the need for reauthorization
$settings['accessToken']        = $accessToken;
$settings['accessTokenSecret']  = $accessTokenSecret; //for OAuth1.0a
$settings['accessTokenExpires'] = $accessTokenExpires; //UNIX timestamp
$settings['refreshToken']       = $refreshToken;
*/

// Initiate the auth object
$initAuth = new ApiAuth();
$auth = $initAuth->newAuth($settings);

// Initiate process for obtaining an access token; this will redirect the user to the $authorizationUrl and/or
// set the access_tokens when the user is redirected back after granting authorization

// If the access token is expired, and a refresh token is set above, then a new access token will be requested
/*
try {
    if ($auth->validateAccessToken()) {

        // Obtain the access token returned; call accessTokenUpdated() to catch if the token was updated via a
        // refresh token

        // $accessTokenData will have the following keys:
        // For OAuth1.0a: access_token, access_token_secret, expires
        // For OAuth2: access_token, expires, token_type, refresh_token

        if ($auth->accessTokenUpdated()) {
            $accessTokenData = $auth->getAccessTokenData();

            //store access token data however you want

	        dump($accessTokenData);
        }
    }
} catch (Exception $e) {
	// Do Error handling
	dump($e);
}
*/

$api = new \Mautic\MauticApi();
$contactApi = $api->newApi('contacts', $auth, $settings['baseUrl'] . '/api');

$fields = $contactApi->getFieldList();

$data = array();

$data['email'] = 'some@email.com';

// Set the IP address the contact originated from if it is different than that of the server making the request
$data['ipAddress'] = '';

// Create the contact
$response = $contactApi->create($data);
var_dump($response);

/**
 array (size=3)
'errors' =>
array (size=1)
0 =>
array (size=3)
'message' => string 'API authorization denied.' (length=25)
'code' => int 401
'type' => string 'access_denied' (length=13)
'error' => string 'access_denied' (length=13)
'error_description' => string 'API authorization denied. (<code>error</code> and <code>error_description</code> are deprecated as of 2.6.0 and will be removed in 3.0. Use the <code>errors</code> array instead.)' (length=146)
 */

// $contact = $response[$contactApi->itemName()];

Where you want to integrate this API ?

Both projects are on localhost - my dev notebook

Problem solved - callback_url was different in mautic and app (they must be the same…!)

hi I am planning to integrate mautic in ecommerce script.
would you please send me your sample code to me.

to gain some idea regarding it.