googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.38k stars 1.91k forks source link

Port errors #3338

Open ntalamdotcom opened 1 year ago

ntalamdotcom commented 1 year ago

1) Is this a client library issue or a product issue? My nextJS has a default port configuration, however...

When executing the following code in NextJS, I get "bind EADDRINUSE null:3000" error:

`/**

2) Did someone already solve this? No one. a couples of days wasted.

3) Do you have a support contract? I do not

Steps to reproduce

  1. ? Create and endpoint using NextJS as trivial: /pages/api/calendars/list-calendars
  2. ? run it with npm run dev or npm run build / start

My packages:

{
  "name": "againweb",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev3001": "next dev -p 3001",
    "dev": "next dev",
    "push": "git push",
    "commit": "git commit -m 'some change'",
    "build": "next build",
    "start": "next start",
    "typecheck": "tsc --noEmit",
    "lint": "next lint"
  },
  "browser": {
    "fs": false,
    "os": false,
    "path": false
  },
  "dependencies": {
    "@emotion/react": "^11.10.5",
    "@emotion/styled": "^11.10.5",
    "@google-cloud/local-auth": "^2.1.0",
    "@googleapis/calendar": "^3.0.0",
    "@mui/icons-material": "^5.10.16",
    "@mui/material": "^5.10.16",
    "@react-oauth/google": "^0.11.1",
    "@types/node": "18.11.10",
    "@types/react": "18.0.26",
    "@types/react-dom": "18.0.9",
    "brotli-webpack-plugin": "^1.1.0",
    "compression-webpack-plugin": "^10.0.0",
    "depcheck": "^1.4.3",
    "dotenv": "^16.0.3",
    "expo-jwt": "^1.4.1",
    "formidable": "^2.1.1",
    "google-auth-library": "^9.0.0",
    "googleapis": "^105.0.0",
    "highcharts-react-official": "^3.2.0",
    "jsonwebtoken": "^9.0.0",
    "jwt-decode": "^3.1.2",
    "mysql": "github:mysqljs/mysql",
    "next": "13.1.1",
    "next-auth": "^4.18.7",
    "node-id3": "^0.2.6",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "typescript": "4.9.4",
    "wordpress-hash-node": "^1.0.0"
  },
  "devDependencies": {
    "@types/babel__preset-env": "^7.9.2",
    "@types/crypto-js": "^4.1.1",
    "@types/express": "^4.17.14",
    "eslint": "^8.45.0",
    "eslint-config-next": "^13.4.12",
    "find-unused-exports": "^5.0.0"
  }
}

Thanks!

alexander-fenster commented 1 year ago

bind EADDRINUSE null:3000

It seems you are using @google-cloud/local-auth, which is pretty much a sample code to show how to implement OAuth2. After logging in, your browser gets redirected to https://localhost:3000, which is the port that local-auth will listen on.

Since @google-cloud/local-auth is a sample package, it does not have any way to change the port it listens on. You might just copy the code - it's just one file - to your project, and use a different port there, other than 3000.

ntalamdotcom commented 1 year ago

bind EADDRINUSE null:3000

It seems you are using @google-cloud/local-auth, which is pretty much a sample code to show how to implement OAuth2. After logging in, your browser gets redirected to https://localhost:3000, which is the port that local-auth will listen on.

Since @google-cloud/local-auth is a sample package, it does not have any way to change the port it listens on. You might just copy the code - it's just one file - to your project, and use a different port there, other than 3000.

Hi Alexander, thank you for your fast reply.

After copying and pasting the block of code you suggested me: 1.- I have changed the port. 2.- I had a problem reading the json credentials file. I had too turn in it into a relative path.

To understand more how to use this, please guide me with this...

I got an error when running this on Windows10: error - uncaughtException: Error: spawn undefined\System32\WindowsPowerShell\v1.0\powershell ENOENT at ChildProcess._handle.onexit (node:internal/child_process:286:19) at onErrorNT (node:internal/child_process:484:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { errno: -4058, code: 'ENOENT', syscall: 'spawn undefined\\System32\\WindowsPowerShell\\v1.0\\powershell', path: 'undefined\\System32\\WindowsPowerShell\\v1.0\\powershell',

Then, when running it on Debian Linux, whatever is the script asking to be execute, runs properly. However it hangs when asking for the confirmation from the url https://accounts.google.com/o/oauth2/v2/auth?redirect_uri...

alexander-fenster commented 1 year ago

re: hangs when asking for confirmation from the URL - it is supposed to open the browser to open that accounts.google.com URL, and then be redirected to your local port (which will pass the authentication code from accounts.google.com to your code). It might not work if you run it over ssh, you need to be able to open browser in the place where it's run.

As for the Windows error, apparently it tries to run Powershell from the wrong path spawn undefined\System32\WindowsPowerShell\v1.0\powershell - note the undefined part where it should say C: or something. I don't have a Windows machine nearby to try that, but some googling shows issues like https://github.com/vitest-dev/vitest/issues/1870. I would assume it's not us, but something with Node.js and child_process on Windows maybe? There are some tricks in that issue such as making sure the powershell.exe is in your PATH, or things like that. Mind trying to figure that out?