当我尝试在节点中执行 https post 请求时,出现 getaddrinfo ENOTFOUND 错误

When I try to do a https post request in node, I get a getaddrinfo ENOTFOUND error

完整的错误是这样的:

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND https://api.instagram.com https://api.instagram.com:443
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:79:26)

我的错误代码是这样的:

var express = require('express');
var app = express();
var https = require('https');

app.get('/oauth/ig', function (req, res) {
  var options = {
    hostname: 'https://api.instagram.com',
    path: '/oauth/access_token?client_secret=myClientSecret&grant_type=authorization_code&redirect_uri=http://localhost:1992/oauth/ig&code='+req.query.code,
    method: 'POST',
  };
  var igRequest = https.request(options, (response) => {
    res.send("response");
  });
})

我正在使用 nodejs 并正在尝试进行 Instagram OAUTH。

hostname 应该是 只是 主机名,不包括协议:

hostname: 'api.instagram.com',

除了mscdex的回答,值得一提的是 npm 上已经有工作模块可以做到这一点,例如:

也许您可以使用其中之一,而不是推出自己的解决方案。

推出自己的解决方案并没有错,但如果您做不到,有时就是不值得这么麻烦。

我上面推荐的两个模块是 Instagram 的 Passport 策略,这是在 Node 中进行身份验证的事实上的标准方式,有超过 300 种不同的策略可供您以统一的方式使用。

有关详细信息,请参阅 Passport 网站。