检索访问令牌 FetchError 时出错:对 https://oauth2.googleapis.com/token 的请求失败,原因:读取 ECONNRESET

问题描述

我想在 vscode 软件中登录 Gdrive。我按照 url: https://developers.google.com/drive/api/v3/quickstart/nodejs 的步骤来做。但是当我运行关于 node 的第 4 步时。 在终端中,出现了这个问题。谁能告诉我怎么解决


    PS D:\VScode> node .
    Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2fdrive.Metadata.readonly&response_type=code&client_id=668097439811-r1jtctrhhnlptug6ghn3snkpckttokgs.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob
    Enter the code from that page here: 4/1AY0e-g7XilzG82JRv7SNkYA0ou6TdmbIYQqZWpWA1ZzuPq6Fc791-1FxA3o
    Error retrieving access token FetchError: request to https://oauth2.googleapis.com/token Failed,reason: read ECONNRESET
        at ClientRequest. (D:\VScode\node_modules\node-fetch\lib\index.js:1461:11)
        at ClientRequest.emit (events.js:315:20)
        at TLSSocket.socketErrorListener (_http_client.js:426:9)
        at TLSSocket.emit (events.js:315:20)
        at emitErrorNT (internal/streams/destroy.js:92:8)
        at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
        at processticksAndRejections (internal/process/task_queues.js:84:21) {
      type: 'system',errno: 'ECONNRESET',code: 'ECONNRESET',config: {
        method: 'POST',url: 'https://oauth2.googleapis.com/token',data: 'code=4%2F1AY0e-g7XilzG82JRv7SNkYA0ou6TdmbIYQqZWpWA1ZzuPq6Fc791-1FxA3o&client_id=668097439811-r1jtctrhhnlptug6ghn3snkpckttokgs.apps.googleusercontent.com&client_secret=vmwOLcfpcTVJQMBBILSfprAB&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&grant_type=authorization_code&code_verifier=',headers: {
          'Content-Type': 'application/x-www-form-urlencoded','User-Agent': 'google-api-nodejs-client/3.1.2',Accept: 'application/json'
        },params: [Object: null prototype] {},paramsSerializer: [Function: paramsSerializer],body: 'code=4%2F1AY0e-g7XilzG82JRv7SNkYA0ou6TdmbIYQqZWpWA1ZzuPq6Fc791-1FxA3o&client_id=668097439811-r1jtctrhhnlptug6ghn3snkpckttokgs.apps.googleusercontent.com&client_secret=vmwOLcfpcTVJQMBBILSfprAB&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&grant_type=authorization_code&code_verifier=',validateStatus: [Function: validateStatus],responseType: 'json'
      }
    }

解决方法

您可以尝试为节点 CLI 环境设置 http/https 代理。

process.env.HTTPS_PROXY = 'Your proxy';
google.options({ proxy: 'Your proxy' });