简体   繁体   中英

HTTPS POST Request in node.js with express.js

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:

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:

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帖子应该发送到端口 443,而不是端口 80。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM