Unable to create Lead through API

Hi everyone,



I’m having some problems trying to create a new Lead through Mautic API and I’m always getting the following error code:

Code:
array(1) { 'error' => array(2) { 'code' => int(403) 'message' => string(45) "access_denied: OAuth2 authentication required" } }

Here is the code I'm using:
Code:
// I'm loading settings from our container ... It contains client key and secret key, ... $settings = $domainero_container['mautic.api.auth_settings'];

$auth = ApiAuth::initiate($settings);

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

$leadApi = MauticMauticApi::getContext(“leads”, $auth, “http://domainero.mautic.com/api/”);

$fields = $leadApi->getFieldList();

$data = array();

foreach ($fields as $f) {
$data[$f[‘alias’]] = ‘’;
}

$data[‘email’] = ‘some-test-email’;

// Create the lead
$lead = $leadApi->create($data);

var_dump($lead);


What's funny is that everything else seems to be working (retrieving lead by id, listing all leads, ...) but creating does not work. I'm not sure what seems to be the problem? Does it have something to do with the fact that we are using Free membership?

Thanks in advance!

Mihailo

Hi everyone,

I’m having some problems trying to create a new Lead through Mautic API and I’m always getting the following error code:

array(1) {
  'error' =>
  array(2) {
    'code' =>
    int(403)
    'message' =>
    string(45) "access_denied: OAuth2 authentication required"
  }
}

Here is the code I’m using:


// I'm loading settings from our container ... It contains client key and secret key, ...
$settings = $domainero_container['mautic.api.auth_settings'];

$auth = ApiAuth::initiate($settings);

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

$leadApi = MauticMauticApi::getContext("leads", $auth, "http://domainero.mautic.com/api/");

$fields = $leadApi->getFieldList();

$data = array();

foreach ($fields as $f) {
    $data[$f['alias']] = '';
}

$data['email'] = 'some-test-email';

// Create the lead
$lead = $leadApi->create($data);

var_dump($lead);

What’s funny is that everything else seems to be working (retrieving lead by id, listing all leads, …) but creating does not work. I’m not sure what seems to be the problem? Does it have something to do with the fact that we are using Free membership?

Thanks in advance!

Mihailo

I think the problem is that you are not saving and using $accessTokenData. Read more at

https://www.mautic.org/blog/how-to-use-the-mautic-rest-api/
https://github.com/mautic/api-library
https://developer.mautic.org/#authorization

If that was the case, I wouldn’t be able to execute other API operations, no? I tried initiating the Auth object with Access & Refreshen token as well, but I got the same response from the API.

I can successfully get list of lids, get lead by specific ID, etc. But all “write” operations (create, update, delete) don’t seem to be working.

Ok I figured it out eventually. Problem was due to the fact that when I was creating an API instance here:

$leadApi = MauticMauticApi::getContext("leads", $auth, "http://domainero.mautic.com/api/");

I was using HTTP protocol instead of HTTPS. Took me a lots of debugging to figure it out LOL. Here’s the code that works:

$leadApi = MauticMauticApi::getContext("leads", $auth, "https://domainero.mautic.com/api/");

Thanks for your help @escopecz !