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.26k stars 1.91k forks source link

Use code to obtain token. During the process of auth2, the request https://oauth2.googleapis.com/token failed. #3450

Open wenshan opened 3 months ago

wenshan commented 3 months ago

I couldn't find the corresponding entry in [support console] and couldn't start, so I'm still here to ask for help. I use googleapi@133.0.0 in my node project, the code is as follows:

const { google } = require('googleapis');

async getToken() { const { ctx } = this; const { code } = ctx.request.body; const { client_id, client_secret, redirect_uri, } = ctx.app.config.oauth2Google; const oauth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri); const data = await oauth2Client.getToken(code); }

Wrong question:

config: {"method":"POST","url":"https://oauth2.googleapis.com/token","data":"< - See errorRedactor option in gaxios for configuration >.","headers":{"Content-Type":"application/x-www-form-urlencoded","User-Agent":"google-api-nodejs-client/9.6.3","x- goog-api-client":"gl-node/20.11.0"},"body":"< - See errorRedactor option in gaxios for configuration>.","responseType":"unknown" } response: undefined error: {"message":"request to https://oauth2.googleapis.com/token failed, reason: read ECONNRESET","type":"system","errno":"ECONNRESET","code":" ECONNRESET"}

Here is the official documentation: https://developers.google.com/identity/protocols/oauth2/web-server?hl=zh-cn#exchange-authorization-code

and code: `const url = require('url'); // Receive the callback from Google's OAuth 2.0 server. if (req.url.startsWith('/oauth2callback')) { // Handle the OAuth 2.0 server response let q = url.parse(req.url, true).query;

// Get access and refresh tokens (if access_type is offline) let { tokens } = await oauth2Client.getToken(q.code); oauth2Client.setCredentials(tokens); }`

Thanks!

wenshan commented 3 months ago

After testing, it was found that this interface https://oauth2.googleapis.com/token There is a problem, this interface does not support SHH requests, and using curl and so on results in the same error.

The following error message: curl: (28) Failed to connect to oauth2. googleapis.com port 443 after 227030 ms: Couldn't connect to server

The execution code is as follows:

curl --location --request POST 'https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjJlZDVhNDMwNGIwOWI4OGI2OGM3NzE1ZTJhZjI0ZWQxNmM1ZDUyNjQifQ.eyJpc3MiOiJzaG9wcGluZ0BhZmZpbGlhdGV0cmFmZmljLTQxNzIxMS5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInN1YiI6InNob3BwaW5nQGFmZmlsaWF0ZXRyYWZmaWMtNDE3MjExLmlhbS5nc2VydmljZWFjY291bnQuY29tIiwic2NvcGUiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9hdXRoL3VzZXJpbmZvLnByb2ZpbGUgaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC91c2VyaW5mby5lbWFpbCBvcGVuaWQgaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9jb250ZW50IiwiYXVkIjoiaHR0cHM6Ly9vYXV0aDIuZ29vZ2xlYXBpcy5jb20vdG9rZW4iLCJpYXQiOjE3MTA0NzUwNTUsImV4cCI6MTcxMDQ3ODY1NX0.dCuiS9yzP0yerAFDIICKNrvzJQbwzbEypP1NGwihqHB7p-QXmz0MMQjx2iLjoLtc6l4oGVVyJH-nIyYTFMaZ1trqnPakKGLqcWHAkOkVmRysjgneela2X2QSj2CW4q9v0YLMhE7wE6P2bIsa1SdEn2wYNxIEEBdBYG8ttFxrQk3mktVwm4Y-IX2r_L1DhZaKadFrWCvN9s2ds__nLuIgaI7CvIJ-6VylfG9g8AdTVx8c2pc0UoZrMwUcqqNPWeeg2hfxwQ-uUOvZVryPo0Dtf3iYH-L9jwgCbd_86s9re4Yz9UsnGVFRZwZA2m_kySAEYwCTQo0gAGcjG_WWjvk2lA' \ --header 'content-type: application/x-www-form-urlencoded' \ --data ''

I found this interface https://oauth2.googleapis.com/token Many people are now giving feedback that I obtain my token access_token through the JWT token, but this interface does not support SSH, which prevents me from obtaining tokens on the node side. The project can only be terminated or alternative solutions can be used.

To be clear, the interface https://oauth2.googleapis.com/token Unsupported SSH caused node

0dvFree commented 1 week ago

Hi, how did you solve it in the end?