Problem with Mautic API oauth2 credentials

Hi,

I cannot connect n8n with Mautic instance using oauth2 credentials.
The problem occurs when accessing URL:
https://n8n.example.com/rest/oauth2-credential/callback?state={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.

UPDATE:
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?’

2 Likes

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:
https://developer.mautic.org/#oauth-2

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.

2 Likes

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

Digging up an old thread, since it describes almost exactly the problem I have.
Mautic and n8n v. 220.0 Oauth2 times out after the set “Access token lifetime (in minutes)”.

Tested one commercially provided Mautic and a self hosted, with the same result. On the commercial I cannot change basic auth, but on the self hosted it is enabled. The account is connected, but fails after the token lifetime with a 401 - if reconnected it works again.

This thread seems simliar: https://community.n8n.io/t/oauth2-token-not-getting-refreshed-throwing-401-after-expiry/ - stating that it is since version 220.0 - I haven’t tested to downgrade.

@RicardoE105 do you think your fix from here has been reverted?

@Jon is this a known issue?

There was a known issue with OAuth failing for some providers a couple of versions ago but that should be resolved in newer releases.

@L-Jo can you open a new thread and complete the template and we can look into it more. For now I am going to close this issue as it is likely to be unrelated with the cause.

2 Likes