节点中的HTTPS POST请求。js和express.js

时间:2021-08-23 02:33:35

I'm having trouble making an https post request in node.js with express.js. I have generated my key.pem and self-signed cert.pem (which are both working), but when I try and make a request to the linkedin API (to get an auth token) I get the following error:

我在node中发出https post请求时遇到了麻烦。与express.js js。我生成了我的钥匙。pem和self-signed 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

I after retrieving an authorization code from linkedin, I am attempting to retrieve an authorization token so that I can make authenticated requests to their api. Here is my node/express file:

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

1 个解决方案

#1


3  

For starters, I would think your https post should be made to port 443, not port 80.

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

#1


3  

For starters, I would think your https post should be made to port 443, not port 80.

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