我在使用express.js在node.js中发布https发布请求时遇到问题。我已经生成了我的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发出经过身份验证的请求。这是我的节点/快递文件:
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'));
});
答案 0 :(得分:3)
对于初学者,我认为你的https
帖子应该是443端口,而不是端口80.