簡體   English   中英

連接到twitter api時,Node.js套接字掛起錯誤

[英]Node.js socket hang up error when connecting to twitter api

當使用https連接到具有基本身份驗證的twitter流API時,我收到了socket hang up錯誤。 我認為在進行身份驗證時會發生錯誤。

var https = require("https");
var options = {
        host: 'stream.twitter.com',
        path: '/1.1/statuses/filter.json?track=bieber',
        method: 'GET',
        headers: {
                "Authorization": "Basic " + new Buffer('UserName' + ":" + 'Password').toString("base64")
        }
}; //end of options


var request = https.request(options, function(response){
            var body = '';
             console.log('STATUS: ' + response.statusCode);
             console.log('HEADERS: ' + JSON.stringify(response.headers));

            response.on("data", function(chunk){
                    var tweet = JSON.parse(chunk);
                    console.log("Tweet " + tweet.text);
            });  // end of data

            response.on("end", function(){
                console.log("Disconnected!!");
            }); // end of end

            response.on("error", function(){
                console.log("error occured");
            }); // end of error
}); // end of request

request.on("error",function(error){
        console.log("Error: " + error.message);
                }); // end of error

當我嘗試訪問此URL時,它工作正常。

https://username:password@stream.twitter.com/1.1/statuses/filter.json?track=twitter

https.request(options, function(response){ }更改為https.get(options, function(response){ }感謝nodejs IRC上的Darrenlooby指出它。

您可以使用https.request,但請確保添加:

request.end();

其中request是從https.request返回的對象

有關示例,請參閱http://nodejs.org/api/https.html#https_https_request_options_callback

我有同樣的問題,這解決了它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM