node.js https.get() 触发错误 ECONNREFUSED

标签 node.js facebook-graph-api https

OAuth 舞蹈的其中一个步骤涉及交换通过回调接收到的代码以获取访问 token 。专门针对 Facebook 服务器端身份验证,以下 https GET 请求在响应正文中返回访问代码:

https://graph.facebook.com/oauth/access_token?
  client_id=YOUR_APP_ID
  &redirect_uri=YOUR_REDIRECT_URI
  &client_secret=YOUR_APP_SECRET
  &code=CODE_GENERATED_BY_FACEBOOK

尝试连接时,Node.js 会引发错误:

 https.get( /**String | Object*/ options, function ( res ) {
   res.setEncoding( 'utf8' );

   res.on( 'data', function ( data ) {
     // parse response body
   } );

 } ).on( 'error',function ( e ) {
      Log.w( TAG, "Error requesting access_token", e );
 } ).end();

错误是:

{  code: 'ECONNREFUSED',
   errno: 'ECONNREFUSED',
   syscall: 'connect' }

调用与 wget/curl 一起工作正常,所以它不是传出防火墙规则等的问题。 Node 请求有什么问题?

最佳答案

原来问题是 https 证书验证失败。来自 Node 的 https.request() docs :

rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.

除非明确提供,否则 http[s].[request|get] 将使用忽略 tls.connect() 的全局代理选项包括 rejectUnauthorized 标志。获取(或请求)的调用需要这样修改:

var options = require('url').parse( /**String*/ url );
options.rejectUnauthorized = false;
options.agent = new https.Agent( options );

https.get( /**Object*/ options, function ( res ) {
  // handle response & body
} ).on( 'error',function ( e ) {
  // handle errors
} ).end();

关于node.js https.get() 触发错误 ECONNREFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15929167/

相关文章:

javascript - Node rcon 的问题

node.js - 在 Express 中对路线进行分组

node.js - 安装 Node.js 和 NPM 的 Hiccup

python - 为什么node.js读取文件比python快?

ios - 通过 Safari 或 FB App 登录 Facebook 无效

facebook - 如何使用 Facebook Graph API 从我的应用程序获取事件?

asp.net-mvc-3 - ASP.NET MVC3 从 https 重定向到 http

react-native - 我的 native 应用程序在从我的服务器错误 : "type error :Network Request Failed " 获取数据时显示错误

java - 如何强制 java 服务器只接受 tls 1.2 并拒绝 tls 1.0 和 tls 1.1 连接