节点js请求的授权问题

时间:2017-11-20 21:12:23

标签: node.js rest api oauth authorization

我很难对ally / tradeking报价流发出经过身份验证的请求。

我正在使用Node JS发出请求。我尝试了几种方法来提出请求,但出于这个问题的目的,我将使用website e来坚持他们oauth modul中的示例。我想做的是console.log(数据),我期待stream of stock quotes。相反,我未经授权返回401。任何人都可以指出我的方向,以更好地解决这个问题,或提供更正?这是我的代码和服务器响应:

/// CODE ///

var OAuth = require('oauth').OAuth;
var credentials = {
    consumer_key: "xxxxxxxxxxx",
    consumer_secret: "xxxxxxxxxxx",
    access_token: "xxxxxxxxxxx",
    access_secret: "xxxxxxxxxxx"
};
var oa = new OAuth(null, null, credentials.consumer_key, credentials.consumer_secret, "1.0", null, "HMAC-SHA1");
var request = oa.get("https://stream.tradeking.com/v1/market/quotes?symbols=AAPL", 
credentials.access_token, 
credentials.access_secret);
request.on('response', function (response) {
    response.setEncoding('utf8');
        console.log(response.statusCode)
        console.log(response)
});
request.end();

/// RESPONSE ///

401

IncomingMessage {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: true,
decoder:
StringDecoder {
encoding: 'utf8',
fillLast: [Function: utf8FillLast],
lastNeed: 0,
lastTotal: 0,
lastChar: <Buffer 00 00 00 00> },
encoding: 'utf8' },
readable: true,
domain: null,
_events: { end: [Function: responseOnEnd] },
_eventsCount: 1,
_maxListeners: undefined,
socket:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'stream.tradeking.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 539,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: [Object],
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /v1/market/quotes?symbols=AAPL HTTP/1.1\r\nAuthorization: OAuth oauth_consumer_key="xxxxxxxxxxxxxxxxxx",oauth_nonce="O0hLnA9vVHvI3KxCDiU23A4QjQxjiWGI",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1511206636",oauth_token=" xxxxxxxxxxxxxxxxxx ",oauth_version="1.0",oauth_signature=" xxxxxxxxxxxxxxxxxx "\r\nHost: stream.tradeking
.com\r\nAccept: */*\r\nConnection: close\r\nUser-Agent: Node authentication\r\nContent-length: 0\r\nContent-Type: application/x-www-form-url
encoded\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/v1/market/quotes?symbols=AAPL',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'stream.tradeking.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 539,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: [Object],
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /v1/market/quotes?symbols=AAPL HTTP/1.1\r\nAuthorization: OAuth oauth_consumer_key=" xxxxxxxxxxxxxxxxxx ",oauth_nonce="O0hLnA9vVHvI3KxCDiU23A4QjQxjiWGI",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1511206636",oauth_token=" xxxxxxxxxxxxxxxxxx ",oauth_version="1.0",oauth_signature=" xxxxxxxxxxxxxxxxxx "\r\nHost: stream.tradeking
.com\r\nAccept: */*\r\nConnection: close\r\nUser-Agent: Node authentication\r\nContent-length: 0\r\nContent-Type: application/x-www-form-url
encoded\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/v1/market/quotes?symbols=AAPL',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
httpVersionMajor: 1,
httpVersionMinor: 1,
httpVersion: '1.1',
complete: false,
headers:
{ date: 'Mon, 20 Nov 2017 19:37:36 GMT',
'content-type': 'text/html',
'transfer-encoding': 'chunked',
connection: 'close',
'set-cookie': [ '__cfduid=d85337d9d3c2b7ad190e48a1bb3eb25f41511206655; expires=Tue, 20-Nov-18 19:37:35 GMT; path=/; domain=.tradeking.c
om; HttpOnly' ],
server: 'cloudflare-nginx',
'cf-ray': '3c0ddd5f2f197961-SEA' },
rawHeaders:
[ 'Date',
'Mon, 20 Nov 2017 19:37:36 GMT',
'Content-Type',
'text/html',
'Transfer-Encoding',
'chunked',
'Connection',
'close',
'Set-Cookie',
'__cfduid=d85337d9d3c2b7ad190e48a1bb3eb25f41511206655; expires=Tue, 20-Nov-18 19:37:35 GMT; path=/; domain=.tradeking.com; HttpOnly',
'Server',
'cloudflare-nginx',
'CF-RAY',
'3c0ddd5f2f197961-SEA' ],
trailers: {},
rawTrailers: [],
upgrade: false,
url: '',
method: null,
statusCode: 401,
statusMessage: 'Unauthorized',
client:
TLSSocket {
_tlsOptions:
{ pipe: null,
secureContext: [Object],
isServer: false,
requestCert: true,
rejectUnauthorized: true,
session: undefined,
NPNProtocols: undefined,
ALPNProtocols: undefined,
requestOCSP: undefined },
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events:
{ close: [Object],
end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
secure: [Function],
free: [Function: onFree],
agentRemove: [Function: onRemove],
drain: [Function: ondrain],
error: [Function: socketErrorListener],
data: [Function: socketOnData] },
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_parent: null,
_host: 'stream.tradeking.com',
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: false,
needReadable: true,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 539,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl:
TLSWrap {
bytesRead: 1369,
_externalStream: {},
fd: -1,
_parent: [Object],
_parentWrap: undefined,
_secureContext: [Object],
reading: true,
owner: [Circular],
onread: [Function: onread],
writeQueueSize: 1,
onhandshakestart: [Function],
onhandshakedone: [Function],
onocspresponse: [Function],
onerror: [Function] },
_requestCert: true,
_rejectUnauthorized: true,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Circular],
incoming: [Circular],
outgoing: [Object],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
_httpMessage:
ClientRequest {
domain: null,
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: [Object],
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Circular],
connection: [Circular],
_header: 'GET /v1/market/quotes?symbols=AAPL HTTP/1.1\r\nAuthorization: OAuth oauth_consumer_key=" xxxxxxxxxxxxxxxxxx”,oauth_nonce="O0hLnA9vVHvI3KxCDiU23A4QjQxjiWGI",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1511206636",oauth_token=" xxxxxxxxxxxxxxxxxx ",oauth_version="1.0",oauth_signature=" xxxxxxxxxxxxxxxxxx "\r\nHost: stream.tradeking
.com\r\nAccept: */*\r\nConnection: close\r\nUser-Agent: Node authentication\r\nContent-length: 0\r\nContent-Type: application/x-www-form-url
encoded\r\n\r\n',
_headers: [Object],
_headerNames: [Object],
_onPendingData: null,
agent: [Object],
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/v1/market/quotes?symbols=AAPL',
_ended: false,
parser: [Object],
res: [Circular] },
read: [Function],
_consuming: true },
_consuming: false,
_dumped: false,
req:
ClientRequest {
domain: null,
_events:
{ response: [Function],
prefinish: [Function: requestOnPrefinish] },
_eventsCount: 2,
_maxListeners: undefined,
output: [],
outputEncodings: [],
outputCallbacks: [],
outputSize: 0,
writable: true,
_last: true,
upgrading: false,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedHeader: { connection: false, 'content-length': false },
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'stream.tradeking.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 539,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
connection:
TLSSocket {
_tlsOptions: [Object],
_secureEstablished: true,
_securePending: false,
_newSessionPending: false,
_controlReleased: true,
_SNICallback: null,
servername: null,
npnProtocol: false,
alpnProtocol: false,
authorized: true,
authorizationError: null,
encrypted: true,
_events: [Object],
_eventsCount: 10,
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: 'stream.tradeking.com',
_readableState: [Object],
readable: true,
domain: null,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 539,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: undefined,
_server: null,
ssl: [Object],
_requestCert: true,
_rejectUnauthorized: true,
parser: [Object],
_httpMessage: [Circular],
read: [Function],
_consuming: true },
_header: 'GET /v1/market/quotes?symbols=AAPL HTTP/1.1\r\nAuthorization: OAuth oauth_consumer_key=" xxxxxxxxxxxxxxxxxx 
",oauth_nonce=" xxxxxxxxxxxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1511206636",oauth_token=" xxxxxxxxxxxxxxxxxx 
",oauth_version="1.0",oauth_signature=" xxxxxxxxxxxxxxxxxx "\r\nHost: stream.tradeking.co
m\r\nAccept: */*\r\nConnection: close\r\nUser-Agent: Node authentication\r\nContent-length: 0\r\nContent-Type: application/x-www-form-urlenc
oded\r\n\r\n',
_headers:
{ authorization: 'OAuth oauth_consumer_key=" xxxxxxxxxxxxxxxxxx ",oauth_nonce="O0hLnA9vVHvI3KxCDiU23A4QjQxjiWG
I",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1511206636",oauth_token=" xxxxxxxxxxxxxxxxxx ",oauth_version=
"1.0",oauth_signature=" xxxxxxxxxxxxxxxxxx "',
host: 'stream.tradeking.com',
accept: '*/*',
connection: 'close',
'user-agent': 'Node authentication',
'content-length': 0,
'content-type': 'application/x-www-form-urlencoded' },
_headerNames:
{ authorization: 'Authorization',
host: 'Host',
accept: 'Accept',
connection: 'Connection',
'user-agent': 'User-Agent',
'content-length': 'Content-length',
'content-type': 'Content-Type' },
_onPendingData: null,
agent:
Agent {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
defaultPort: 443,
protocol: 'https:',
options: [Object],
requests: {},
sockets: [Object],
freeSockets: {},
keepAliveMsecs: 1000,
keepAlive: false,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
_sessionCache: [Object] },
socketPath: undefined,
timeout: undefined,
method: 'GET',
path: '/v1/market/quotes?symbols=AAPL',
_ended: false,
parser:
HTTPParser {
'0': [Function: parserOnHeaders],
'1': [Function: parserOnHeadersComplete],
'2': [Function: parserOnBody],
'3': [Function: parserOnMessageComplete],
'4': null,
_headers: [],
_url: '',
_consumed: false,
socket: [Object],
incoming: [Circular],
outgoing: [Circular],
maxHeaderPairs: 2000,
onIncoming: [Function: parserOnIncomingClient] },
res: [Circular] } }

1 个答案:

答案 0 :(得分:0)

Ally invest有一个秘密小盒子需要检查以允许流媒体报价。必须导航到他们的配置文件设置,找到并选中此框以允许API流式传输。目前尚不清楚这是否必须发生在API流中,我要花3个月来计算出来!如果这对您有帮助,欢迎!!!!!!!!!