使用express.js在node.js中进行HTTPS POST请求

韦伯斯特

我在用express.js在node.js中发出https发布请求时遇到麻烦。我已经生成了key.pem和自签名cert.pem(它们都可以工作),但是当我尝试向linkedin API发出请求(以获取auth令牌)时,出现以下错误:

events.js:72

13:51:36 web.1  |         throw er; // Unhandled 'error' event
13:51:36 web.1  |               ^
13:51:36 web.1  | Error: 140735143060224:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:787:
13:51:36 web.1  |
13:51:36 web.1  |     at SlabBuffer.use (tls.js:234:18)
13:51:36 web.1  |     at CleartextStream.read [as _read] (tls.js:454:29)
13:51:36 web.1  |     at CleartextStream.Readable.read (_stream_readable.js:340:10)
13:51:36 web.1  |     at EncryptedStream.write [as _write] (tls.js:368:25)
13:51:36 web.1  |     at doWrite (_stream_writable.js:225:10)
13:51:36 web.1  |     at writeOrBuffer (_stream_writable.js:215:5)
13:51:36 web.1  |     at EncryptedStream.Writable.write (_stream_writable.js:182:11)
13:51:36 web.1  |     at write (_stream_readable.js:601:24)
13:51:36 web.1  |     at flow (_stream_readable.js:610:7)
13:51:36 web.1  |     at Socket.pipeOnReadable (_stream_readable.js:642:5)
13:51:36 web.1  | exited with code 8
13:51:36 system | sending SIGTERM to all processes

我从linkedin中检索了授权代码后,我试图检索授权令牌,以便可以向其api发出经过身份验证的请求。这是我的节点/表达文件:

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

var url = require('url');
var querystring = require('querystring');

// var http = require('http');
var https = require('https');
var fs = require('fs');

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

app.get('/', function(request, response) {
    // requests allowed from...
    response.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
    // Request methods you wish to allow
    response.setHeader('Access-Control-Allow-Methods', 'GET, POST');

    // set response type
    response.writeHead(200);

    // // // get query
    var url_parts = url.parse(request.url, true);
    var query = url_parts.query;
    var auth_code = query.authcode;

    // Make request to linkedin api
    var post_data = querystring.stringify({
      'grant_type' : 'authorization_code',
      'code': auth_code,
      'redirect_uri': 'http://localhost:4200/authenticated',
      'client_id': '*************',
      'client_secret': '****************'
    });

    // An object of options to indicate where to post to
    var post_options = {
      host: 'www.linkedin.com',
      port: '80',
      path: '/uas/oauth2/accessToken',
      method: 'POST'
    };

    // Set up the request
    var post_req = https.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response: ' + chunk);
      });
    });

    // post the data
    post_req.write(post_data);
    post_req.end();

    object = {"token": auth_code};
    output = JSON.stringify(object);

    response.write(output);
    response.end();
});

https.createServer({
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
}, app).listen(app.get('port'), function() {
    console.log('listening on port ' + app.get('port'));
});
jfriend00

对于初学者,我认为您的https帖子应该发布到端口443,而不是端口80。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Express的Node.js:如何重定向POST请求

使用Node.js异步请求在Redis中进行循环

无法接收来自Post请求的Node.js响应(不使用Express)

使用Node,Express和Vanilla JS进行POST Ajax调用

Express js:如何使用POST请求下载文件

如何使用https-post将node.js的POST请求发送到php页面?

使用 Node Js 和 express 调用来自 rest api 的 post 请求时出错。请求数据变空

如何使用 Node.JS 向 Ghostbin 发出 POST 请求?

在Node.js中使用POST请求上传文件

如何使用node.js和express.js对网站启用TLS和https请求?

侦听对EC2上的3000端口进行HTTPS请求的Node.js Express应用

如何使用 Node.js 发出 https post 请求(需要授权)

使用Node.js和Express发出unirest GET请求

使用 PhpStorm 在 node.js Express 中调试 HTTP 请求

从 AngularJS $http/AJAX 到基于 Node.js + Express.js 的 API 的 POST、PUT、DELETE 请求

使用Node.js在Azure Functions中进行HTTP Post调用

在node.js中使用请求模块时,POST请求状态代码(500)。Postman中的POST请求工作正常

无法使用Node.js和Express捕获POST参数

使用 AJAX Post 和 Node.js Express

使用Node.js / Express访问AJAX POST数据

使用Node.js进行POST请求时,如何获取请求有效内容的内容长度?

使用Node.js和Express进行发布时如何访问请求正文?

在GridFS,Express,mongoDB,node.js中存储来自POST请求的数据流

Node.js / Express POST请求正文已解析为错误的JSON

Node.JS POST请求通过护照保护的Express应用程序

表单POST请求正文为空-Node.js / express / ejs / body-parser

如何调试对Node.js Express应用程序的POST请求?

我不断从 node js express post 请求正文中获取未定义

修改请求正文,然后在Node.js中进行代理