Google oAuth callback error: localhost refused connetion

Describe the problem/error/question

I have the latest version of n8n running in a docker container on localhost:5678
I have used this OAuth Redirect URL in the Google Auth: http://localhost/rest/oauth2-credential/callback

What is the error message (if any)?

This site can’t be reached

localhost refused to connect.

Try:

ERR_CONNECTION_REFUSED

I am not using any firewalls.

I a not a developer just followed the steps in the tutorial and guides.

  • n8n version: Version 1.78.1
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):**Docker
  • **Operating system:**MacOS

@Check_Shi, welcome to community. Check this tread please:
Local Host setup
If you have additional questions I am happy to help.

You need to specify the port as well, try:

http://localhost:5678/rest/oauth2-credential/callback

I Think even with a port the issue wont be solved, as he didn’t mentioned about networking setup option of the docker or the system.

Your issue is Google can’t reach http://localhost:5678/rest/oauth2-credential/callback since localhost is local-only. In your Docker setup, update the Google OAuth redirect URI to http://host.docker.internal:5678/rest/oauth2-credential/callback (works on MacOS Docker), or use ngrok to expose localhost publicly and set the URI to the ngrok URL (e.g., http://your-ngrok-id.ngrok.io/rest/oauth2-credential/callback). Restart n8n after adjusting.

Google doesn’t need to connect to that URL. Google will only redirect to that URL. As long as the URL is valid and you can connect to it locally, it will work. It works for me locally as well. There’s no need to forward or expose any network.

If you mean to make this change in the URI in the Google cloud I have already tried it but I got a 400 error. And I am not sure how to change the redirecting URl on n8n it doesn’t give the option to edit on the setup stage. Do you know if it is possible?
Error 400: redirect_uri_mismatch

I thought as well. Because it is a redirect. So I wanted to make sure that this works before setting an external domain.

Yes, the URL in Google Cloud needs to be

http://localhost:5678/rest/oauth2-credential/callback

This error means that the URL configured in Google Cloud is not the same as the one n8n initiated a request from. n8n will send

http://localhost:5678/rest/oauth2-credential/callback

in the request, so the redirect URL in Google Cloud must be the same.

As long as Docker is running, and you’re able to access localhost:5678 locally, the callback URL should also work.

Here’s a screen recording demonstrating it working:

Thanks for sharing this screen recording! that is exactly my problem. See the screenshot below from the credentials:


So this is different from the Google OAuth URI (which has the :PORT). Now I have tried to change this in the .yml
n8n:
image: docker.n8n.io/n8nio/n8n
container_name: n8n
restart: always
ports:
- 5678:5678
environment:
- N8N_HOST=localhost
- N8N_PORT=5678
- WEBHOOK_URL=http://localhost:5678
volumes:
- n8n_data:/home/node/.n8n

volumes:
n8n_data:
But I am noot sure if I am doing the write thing or where else can I change it so that the
Redirect URL changess from: http://localhost to http://localhost:5678 on n8n’s credentials flow.

This is interesting. Are you using n8n on localhost:5678? I wonder how n8n is getting localhost as the redirect URL.

Could you try to create a new container from scratch without any env vars and with a fresh volume to see if you get it working in there?

2 Likes

Thanks so much! not sure what was wrong in the first installation but now I that I have reinstalled it, the redirect is updated with the port. Thanks a lot. :relieved:

Hello ! I’ve got the same kind of issue here but I’m using n8n on my NAS so I connect to it using 192.168.1.22:5678.
I used webhook_URL to change it in my configuration but Google doesn’t accept http://192.168.1.22:5678/rest/oauth2-credential/callback as a redict URL.

Any idea how to solve this issue ? Thanks a lot !

You’d likely have to setup a proxy server like the one described above to forward the connection to that IP. Or, you can complete Google OAuth on localhost and then continue using n8n on your NAS.

Hello, thanks for the quick reply !
What do you mean by complete on localhost ? Is there a way to complete Google OAuth outside the NAS ?

Yeah, you can move n8n data to your device from NAS, then login to Google on localhost and then move the data back to NAS.

I’m not exactly sure on what folders you’d have to move, though I believe moving .n8n folder should be enough.

So basically, I should run a self hosted version of n8n outside my NAS container directly on my PC and then export the .n8n back to the NAS docker to have credentials set up ?

Yes, either that, or forward your NAS IP to a domain like pointed above. Sadly, it’s a Google limitation of not allowing IPs as callback URLs, so there’s nothing n8n can do to solve that.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.