如何调试错误:安全API请求上的getaddrinfo ENOTFOUND

时间:2019-06-30 00:05:56

标签: node.js oauth

我正在使用YNAB(您需要预算)API和端口3000上的ngrok构建应用程序的原型,并收到以下错误。目前,我只是在设置基本路由,但无法成功接收访问令牌并无法使浏览器重定向回我的“ / app”路由。下面的错误是控制台日志;我还在浏览器中收到502。

{ Error: getaddrinfo ENOTFOUND https://app.youneedabudget.com https://app.youneedabudget.com:443
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://app.youneedabudget.com',
  host: 'https://app.youneedabudget.com',
  port: 443 }

我用于YNAB的OAuth请求的API文档可以是found here。我正在使用主机名为https://ynag.ngrok.io的ngrok set,并将重定向网址设置为:

https://ynag.ngrok.io/oauth/token

https://ynag.ngrok.io/app

https://ynag.ngrok.io/oauth/redirect

我在终端中使用export命令添加我的客户端ID和密码,并成功进入YNAB的API登录页面,检索授权代码,并重定向到“ / oauth / token”(由我的控制台日志验证)和打开的授权页面),但是在单击“授权”以接收访问令牌并重定向到我的“ / app”路由后,我收到上述错误,并且该页面未重定向或表示502 Bad Gateway。

// index.js

const express = require('express');
const querystring = require('querystring');
const https = require('https');
const morgan = require('morgan');

const clientID = process.env.clientID;
const clientSecret = process.env.clientSecret;
const codeRedirect = encodeURI('https://ynag.ngrok.io/oauth/token');
const tokenRedirect = encodeURI('https://ynag.ngrok.io/app');

const app = express();
const port = process.env.PORT || 3000;

let authCode;

app.use(morgan('dev'));

// Home Route
app.get('/', (req, res) => {
  res.send('App Home');
});

// Obtain Authorization Code
app.get('/oauth/redirect', (req, res) => {

  res.redirect(`https://app.youneedabudget.com/oauth/authorize?client_id=${clientID}&redirect_uri=${codeRedirect}&response_type=code`);

});

// Obtain Access Token
app.get('/oauth/token', (req, res) => {

  authCode = req.query.code;
  console.log(`Client ID: ${clientID} \nClient Secret: ${clientSecret} \nAuthorization Code: ${authCode}`);

  let post_data = querystring.stringify({
    'client_id': clientID,
    'client_secret': clientSecret,
    'redirect_url': tokenRedirect,
    'grant_type': 'authorization_code',
    'code': authCode
  });

  let post_options = {
    host: 'https://app.youneedabudget.com',
    path: `/oauth/token?client_id=${clientID}&client_secret=${clientSecret}&redirect_uri=${tokenRedirect}&grant_type=authorization_code&code=${authCode}`,
    port: 443,
    method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': post_data.length
    }
  };

  let post_req = https.request(post_options, (res) => {
    console.log('Status: ' + res.statusCode);
    console.log('Headers: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', (response) => {
      console.log(`Response: ${response}`);
    });
  });

  post_req.on('error', (e) => {
    console.error(e);
  });

  post_req.write(post_data);
  post_req.end();

});

// Successful Login Route
app.get('/app', (req, res) => {
  res.send('Logged in.');
});

app.listen(port, () => {
  console.log(`Server listening on port ${port}.`);
});

0 个答案:

没有答案