I need a For Dummies guide to get the Mautic API properly configured

The problem might be that I dont have the experience/understanding of OAuth2 or I’m just having a rough time following the available instructions; likely both. Im trying to get the Mautic API configured so that I can read/write lead information as needed.



My environment:

Mautic v1.4.0

Server-based install on LAMP stack

  • PHP v5.4.16
  • MySql v5.5.44-MariaDB
  • cURL enabled
  • SSL enabled/implemented

    Mautic status: appears fully functional



    Instruction sources:

    A) https://github.com/mautic/api-library#authorization

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

    C) https://developer.mautic.org/?php#oauth-2



    Done so far:

    ~1. In Mautic UI -> Configuration -> API Settings:
  • API enabled: yes
  • Access token lifetime: 60
  • Refresh token lifetime: 14

    ~2. In Mautic UI -> API Credentials, created a new entry with:
  • Name: myapi
  • Redirect URL: https://mydomain.com
  • Client ID: clientid_123
  • Client Secret: clientsecret123

    ~3. Downloaded a .zip of the API library from github and extracted

    ~4. Uploaded the contents of “lib” folder to /path/to/Mautic/lib/

    ~5. Created a file with the code to obtain an access token, as instructed via instruction source A, and saved to /path/to/Mautic/apiinit.php

    ~6. Uploaded the contents of “apitester” folder to /path/to/Mautic/apitester/



    Results:
  1. Navigating to the file from step ~5 above results in (with and without hardcoding values for the $settings array) :
  • A server 500 error in the browser
  • The following error in my server log: PHP Fatal error: Class ‘Mautic\Auth\ApiAuth’ not found in /path/to/Mautic/apiinit.php on line 28

    Line 28: $auth = ApiAuth::initiate($settings);
  1. Navigating the the apitester results in the following message displayed in the browser (the expected form is not displayed at any point):

    {“error”:“access_denied”,“error_description”:“OAuth2 authentication required”}



    Questions:

    ~1. Have I placed the contents of the “lib” and “apitester” folders in the right place on my server and, if not, where do these go?

    ~2. Instruction source C, under Libraries -> PHP Library, says to add the following line to the project, but doesnt specify exactly where:

    require_once DIR . ‘/lib/Mautic/MauticApi.php’; //Which folder/file/line does this code get added to?

    ~3. How do I resolve the two Result issues?

    ~4. What else might I be missing or getting wrong?



    Sorry for the long message and thanks in advance for any help.

Somebody, haha, throw me a life raft… please

Hello Pierre, Does my answer offer you any value?

Anybody that was able to get API functionality up and running, your help here would be greatly appreciated.

Hello Steven,

Your 500 error come from Mautic or API Tester ?
Do you have test call API from our script (without API Tester interface) ?

Best regards,
Pierre

Hi Pierre,

The 500 error occurs when I navigate to the page on my server that has the script from this page/section: https://github.com/mautic/api-library#authorization

Is this the script you are referring to?

With that script, It appears to be a PHP error relating to a problem with this line in the script: use MauticAuthApiAuth; not being processed properly and causing this line: $auth = ApiAuth::initiate($settings); to fail and produce the error message on the server log.

Please let me know if I understood and was able to answer your question properly.

Is there anybody out there?

The problem might be that I dont have the experience/understanding of OAuth2 or I’m just having a rough time following the available instructions; likely both. Im trying to get the Mautic API configured so that I can read/write lead information as needed.

My environment:
Mautic v1.4.0
Server-based install on LAMP stack

  • PHP v5.4.16
  • MySql v5.5.44-MariaDB
  • cURL enabled
  • SSL enabled/implemented
    Mautic status: appears fully functional

Instruction sources:
A) https://github.com/mautic/api-library#authorization
B) https://www.mautic.org/blog/developer/how-to-use-the-mautic-rest-api/
C) https://developer.mautic.org/?php#oauth-2

Done so far:
~1. In Mautic UI -> Configuration -> API Settings:

  • API enabled: yes
  • Access token lifetime: 60
  • Refresh token lifetime: 14
    ~2. In Mautic UI -> API Credentials, created a new entry with:
  • Name: myapi
  • Redirect URL: https://mydomain.com
  • Client ID: clientid_123
  • Client Secret: clientsecret123
    ~3. Downloaded a .zip of the API library from github and extracted
    ~4. Uploaded the contents of “lib” folder to /path/to/Mautic/lib/
    ~5. Created a file with the code to obtain an access token, as instructed via instruction source A, and saved to /path/to/Mautic/apiinit.php
    ~6. Uploaded the contents of “apitester” folder to /path/to/Mautic/apitester/

Results:

  1. Navigating to the file from step ~5 above results in (with and without hardcoding values for the $settings array) :
  • A server 500 error in the browser
  • The following error in my server log: PHP Fatal error: Class ‘Mautic\Auth\ApiAuth’ not found in /path/to/Mautic/apiinit.php on line 28
    Line 28: $auth = ApiAuth::initiate($settings);
  1. Navigating the the apitester results in the following message displayed in the browser (the expected form is not displayed at any point):
    {“error”:“access_denied”,“error_description”:“OAuth2 authentication required”}

Questions:
~1. Have I placed the contents of the “lib” and “apitester” folders in the right place on my server and, if not, where do these go?
~2. Instruction source C, under Libraries -> PHP Library, says to add the following line to the project, but doesnt specify exactly where:
require_once DIR . ‘/lib/Mautic/MauticApi.php’; //Which folder/file/line does this code get added to?
~3. How do I resolve the two Result issues?
~4. What else might I be missing or getting wrong?

Sorry for the long message and thanks in advance for any help.

I tried using the API with Mautic and finally got it working…

…but felt like I had to hack my way through. I ran the API tester and snatched the token to be used in my php backend application.

The authorization is useless from a backend system because there is no one there to fill in the authorization form!

I wanted to do things like… if a user registers an account on my site, I want to add them as a lead in mautic. This seems fairly simple…but I can’t do that without an authorization token. (again, unless I am missing something).

Im still unclear as to how to do this “properly” without having to use the API tester and grabbing the token generated.

I know this HAS to be possible without an authorization token. (one-legged OAuth?)

The new version of Mautic requires php 7 which is not running on my server and I cannot rebuild an entire box just to use Mautic…

So… I have to run another server simply for running Mautic.

Ill do a clean install (I updated my last installation and nothing worked and lost 17k leads I had created because I was running php 5.4). They are still in my database if I could figure out how to migrate…or this will be a complete loss.

Soooo…giving this another shot. I love what the Mautic guys are doing, I just really really would like to find a really simple solution for talking to Mautic without the need for a popup authorization form. That makes no sense to me unless I am making an administration system and a human is authorizing.

If I am missing something please please please let me know, this would be fantastic to have working with a simple API call.

Keep up the good work guys.
John

Well I “had” it working until the 2.0 update. Now I am getting token_rejected errors and can’t seem to get past it. Im using the API on a server for an iPhone app. The app calls the server, and I wanted to communicate to Mautic to update a lead etc.

I created the OAuth1a tokens successfully, after much hacking, and now I am getting token_rejected.

Wish I could help you, but this is the most difficult API experience I have ever come across.

you have to add in the subdomain folder, can yuo send the version 1.4 for review?

Hi
in app/config/security.php

change ‘api’ in ‘firewalls’ to this:

'api' => [
                'pattern'            => '^/api',
                'http_basic'         => true,
                'fos_oauth'          => false,
                'bazinga_oauth'      => false,
                'mautic_plugin_auth' => false,
                'stateless'          => true,
            ],

clear cache and you can use your admin user/pass with Basic Authentication to call all API methods

1 Like

I had the same problem too, and I ended up using curl to simulate a form post.

But I only needed to save the user info in Mautic, not the other way around.

[quote=12698:@Steven]

I have struggled to get the api setup with basic auth for OVER A YEAR and this comment fixed my issue finally! I wish there was something I could do to promote this comment to the highest level. Thank you @peyman! I can now move on with my life!

1 Like