HTTPS POST node.js 请求 express.js

HTTPS POST Request in node.js with express.js

我在 node.js 中使用 express.js 发出 https post 请求时遇到问题。我已经生成了我的 key.pem 和自签名的 cert.pem (它们都在工作),但是当我尝试向 linkedin API 发出请求(以获得授权令牌)时,我得到以下错误:

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 发出经过身份验证的请求。这是我的 node/express 文件:

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'));
});

首先,我认为您的 https post 应该连接到端口 443,而不是端口 80。