Issue with Google OAuth2 authentication

Hi n8n community,

I’m facing an issue while trying to set up Google OAuth2 credentials in n8n and would appreciate any help or guidance. I’m relatively new to n8n and self-hosting, so step-by-step instructions would be incredibly helpful! Here’s a detailed explanation of the problem:

Setup Details:

  • I’m self-hosting the latest n8n Docker image on Render.com.

  • I’ve followed the official documentation and multiple tutorials to set up Google OAuth2 credentials.

  • My OAuth Redirect URL in the Google Cloud Console is set to:
    http://localhost:5678/rest/oauth2-credential/callback

The Problem:

When I enter my Client ID and Client Secret in the n8n Google Calendar OAuth2 API node and proceed to sign in to my Google account, I:

  1. Select my Google account.

  2. Grant all the required permissions.

  3. Click “Allow.”

At this point, instead of being redirected back to n8n, I’m taken to a page with the following error:
This site can’t be reached localhost refused to connect. ERR_CONNECTION_REFUSED

What I’ve Tried:

  • Verified the OAuth Redirect URL multiple times to ensure it matches the one in the Google Cloud Console.

  • Confirmed that the n8n instance is running and accessible.

  • Checked for any proxy or firewall issues but didn’t find anything blocking the connection.

Additional Context:

  • I’m using Render.com to host n8n, and the instance is running without any other apparent issues.

  • I’m not using a custom domain or any reverse proxies (e.g., Nginx, Traefik).

  • I’ve double-checked the Google Cloud Console settings (e.g., authorized domains, redirect URIs) to ensure they align with the documentation.

Since I’m a complete noob to n8n and self-hosting, I’d really appreciate it if someone could guide me step-by-step to resolve this issue.

If any additional information would help diagnose the issue, please let me know, and I’ll provide it.

Thanks in advance for your help!

I can see exactly what’s wrong! This is a very common issue when self-hosting n8n, and I’ve helped dozens of clients fix this exact problem.

The Problem

You’re hosting n8n on Render.com (a cloud platform), but your OAuth redirect URL is set to:

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

When Google tries to redirect back after OAuth approval, it’s trying to reach localhost (your local computer), not your Render.com instance. That’s why you get ERR_CONNECTION_REFUSED - Google is sending the OAuth callback to a server that doesn’t exist.

The Solution

You need to use your actual Render.com URL in the redirect, not localhost.

Step 1: Set the WEBHOOK_URL Environment Variable on Render

  1. Go to your Render.com dashboard

  2. Select your n8n service

  3. Go to Environment tab

  4. Add this environment variable:

    • Key: WEBHOOK_URL
    • Value: https://your-n8n-app.onrender.com
      (Replace with your actual Render.com URL - find it in the service overview)
  5. Save Changes and redeploy the service

Step 2: Update Google Cloud Console Redirect URI

  1. Go to Google Cloud Console

  2. Select your OAuth 2.0 Client ID

  3. Under Authorized redirect URIs, remove the localhost URL

  4. Add your Render URL:

    https://your-n8n-app.onrender.com/rest/oauth2-credential/callback
    
  5. Save the changes

Step 3: Recreate Credentials in n8n

  1. Delete your existing Google OAuth2 credential in n8n
  2. Create a new Google Calendar OAuth2 API credential
  3. When you open it, n8n will now show the correct callback URL (your Render URL, not localhost)
  4. Enter your Client ID and Secret
  5. Click “Sign in with Google”
  6. This time, the redirect will work!

Why This Happens

By default, n8n uses http://localhost:5678 as the base URL. This works fine for local development, but when you’re hosting on Render (or any cloud platform), you need to tell n8n its public URL using the WEBHOOK_URL environment variable.

Pro Tips for Self-Hosted n8n on Render

Since you’re self-hosting on Render, here are some other critical environment variables you should set:

WEBHOOK_URL=https://your-app.onrender.com
N8N_HOST=your-app.onrender.com
N8N_PROTOCOL=https
N8N_PORT=5678

This ensures all OAuth callbacks, webhooks, and API endpoints use your public Render URL instead of localhost.

Need Help with Production n8n Setup?

I specialize in setting up production-grade n8n instances for businesses, including:

  • OAuth integrations (Google, Slack, HubSpot, etc.)
  • Custom domain configuration
  • SSL certificates and security
  • Database optimization
  • High-availability deployments

I’ve deployed n8n for clients on AWS, GCP, Render, and Railway - happy to help if you need a production-ready setup!

Let me know if this fixes your issue! :rocket:

I can back up this answer as recommended. I have set up n8n with my domain and it uses those environment variables.

1 Like