Posted By

nikefido on 04/23/11


Tagged

nodejs


Versions (?)

Nodejs Make http request with basic authorization for twitter streaming API


 / Published in: JavaScript
 

I needed to make an HTTP request with basic authorization for twitter streaming API. Users Twitter node library - https://github.com/jdub/node-twitter (npm install twitter ). I had to tweak it so I could avoid the complications of using oAuth on an all server-side implementation.

  1. //Create a client (need to pass it these values??)
  2. var client = http.createClient(80, 'stream.twitter.com');
  3. //Request with headers for basic auth, which Twitter also accepts in addition to oAuth
  4. //Note that we're using method 'statuses/filter' and parameter 'track'
  5. //@Link http://dev.twitter.com/pages/streaming_api_methods#statuses-filter
  6. var request = client.request("GET", '/1/statuses/filter.json?track=some_keyword',{
  7. "Host":"stream.twitter.com",
  8. "Authorization":"Basic " + new Buffer('username' + ":" + 'password').toString('base64'), //Need to create buffer object and base64 encode it
  9. "User-Agent": "Twitter-Node"
  10. });
  11.  
  12. request.on('response', function(response) {
  13. response.on('data', function(chunk) {
  14. console.log(chunk); //should be object with tweet information
  15. });
  16. response.on('error', function(error) {
  17. console.log(error);
  18. });
  19. response.on('end', function () {
  20. console.log('request end');
  21. });
  22. });
  23. request.on('error', function(error) {
  24. console.log(error);
  25. });
  26. request.end();

Report this snippet  

You need to login to post a comment.