Problem with Mautic API oauth2 credentials


I cannot connect n8n with Mautic instance using oauth2 credentials.
The problem occurs when accessing URL:{state}&code={code}
On the n8n side, I get the following error:

(node:61926) UnhandledPromiseRejectionWarning: Error: HTTP status 403
at .../.npm-global/lib/node_modules/n8n/node_modules/client-oauth2/src/client-oauth2.js:283:25
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async .../.npm-global/lib/node_modules/n8n/dist/src/Server.js:852:32

In the console, I can see 302 response from Mautic but next request is failing (probably because of n8n error mentioned above).

Do you have any idea what can be wrong?
BTW Connection via Basic Auth is working.

When the token expires server responses as expected so the problem is only when state and code params returned from Mautic are valid:

{"code":0,"message":"The OAuth2 callback state is invalid!","stack":"ResponseError: The OAuth2 callback state is invalid!\n    at ...../.npm-global/lib/node_modules/n8n/dist/src/Server.js:828:39\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)"}

Thanks Pawel.

@pavvel welcome the community.

I made a small change that might fix the issue. Can you please test it locally and let me know if that fixes the issue for you?’

1 Like

Hi Ricardo,

Yes, this fixed a problem with the connection to Mautic. Thank you!

But I have faced another issue.
In Mautic configuration there are two settings related to API:
Access token lifetime (in minutes) and Refresh token lifetime (in days)

When Access token expires the Refresh token should be used for getting a valid Access token but it is not. When Access token expired I got 403 and the only way to make a request to Mautic again was manually connect in n8n credential settings.

Detailed info you can find in Mautic docs:

Regards, Pawel.

mmm the refresh token expiring would be an issue. Just set the refresh token lifetime to something will not expire anytime soon. I used the default configuration and it’s refreshing the access token just fine every 60 minutes.

I tested it with 1 minute for an Access token and 1 day for Refresh token and after one minute the Mautic node just stopped working while it should use Refresh token for getting new tokens.

The important thing is also that it is not needed to have such a long lifetime for the refresh token because when Access token is refreshed also new Refresh token is returned from Mautic.
So it should be enough to obtain both tokens not less often then Refresh token lifetime (which by default is 14 days and cannot be set to less than 1 day).

Of course when Refresh token expires the only way to refresh both tokens is to manually connect via credential settings.

did it end up working for you?

I thought yes for a moment but I realized that I have changed setting to 10 minutes.
So my previous post is valid.

ahhh I think I found the issue. If you turn enable basic authentication on then it will be able to refresh the token. When refreshing the token n8n is sending the client_id and client_secret using basic authentication but since basic authentication it’s not enable it’s blocking that request, that is why you get the 403.

@pavvel ok made a change so hopefully, basic authentication would not have to be activated in the Mautic side to be able to refresh the credentials. Please pull the latest change and let me know if it fixes all the issues. Thanks.

1 Like

@RicardoE105 It is working now. :muscle:
Thank you!