Node.js and Google Sheets API integration issue: Redirect leads to 'Page not found' error

I’m having trouble setting up the Google Sheets API with Node.js. Here’s what’s happening:

  1. I followed the setup steps in the docs
  2. Changed ‘credentials.installed’ to just ‘credentials’ in the code
  3. When I try to authorize, it asks me to sign in and allow access
  4. After clicking ‘Allow’, I get a ‘Page not found’ error (ERR_CONNECTION_REFUSED)
  5. The URL shows ‘code=4’, but I’m not sure what that means
  6. My console is stuck waiting for a code

I’ve set both ‘Authorised JavaScript origins’ and ‘Authorised redirect URIs’ to ‘http://localhost:8080’ in my project settings.

My credentials.json looks like this:

{
  "client_id": "HIDDEN",
  "project_id": "HIDDEN",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_secret": "HIDDEN",
  "redirect_uris": ["http://localhost:8080"],
  "javascript_origins": ["http://localhost:8080"]
}

Any ideas why I’m getting this error instead of the expected code?

hey tom, i had a similar issue. try clearing ur browser cache and cookies. sometimes the old auth data messes things up. also, double-check ur firewall settings - it might be blocking the redirect. if that doesnt work, maybe try a different port like 3000 instead of 8080. good luck!

I’ve been through this headache before, and it can be frustrating. Have you tried running your Node.js app with elevated privileges? Sometimes, especially on Windows, this can solve permission-related issues with localhost.

Another thing to check is your Google Cloud Console settings. Make sure you’ve enabled the Google Sheets API for your project. It’s easy to overlook this step.

If those don’t work, try using ngrok to create a secure tunnel to your localhost. This can sometimes bypass redirect issues. Just remember to update your authorized redirect URIs in the Google Cloud Console with the ngrok URL.

Lastly, double-check your scopes in the authorization request. If they don’t match exactly with what’s in your Google Cloud Console, it can cause redirect problems. Hope this helps!

I encountered a similar problem when setting up the Google Sheets API with Node.js. One solution that worked for me was to use the ‘http://localhost’ URI without specifying a port number in both the credentials.json file and the Google Cloud Console settings. This approach seemed to resolve the redirect issues.

Additionally, ensure you’re using the latest version of the google-auth-library package. Older versions sometimes have compatibility issues with the current Google OAuth flow. If you’re still facing problems, consider checking your Node.js version - I found that using Node.js v14 or higher improved compatibility with the Google APIs.

Lastly, double-check that your OAuth consent screen is properly configured in the Google Cloud Console. Sometimes, incomplete setup there can lead to unexpected errors during the authorization process.