使用Node JS获取我的推特时间线而不是公共推特时间线

时间:2013-08-14 11:39:53

标签: node.js twitter twitter-oauth

此代码工作正常,但它似乎检索任何推文不仅我跟随的人,我想得到我的时间轴推文 我该怎么办?

var open = require('open');
var sys = require('sys');
var spawn = require('child_process').spawn
var OAuth= require('oauth').OAuth;
var express = require("express");
var util   = require('./util');
var app = express();

var oa = new OAuth(
  "https://api.twitter.com/oauth/request_token",
  "https://api.twitter.com/oauth/access_token",
  "NeFpQZNDapDtFJnUXDe0Q",
  "mXf7AKYJWIdiToKuSgsTm2me0GBA1aNn39uskmEoOM",
  "1.0",
  "http://127.0.0.1:8080/twitterCallback",
  "HMAC-SHA1"
);
var oauthtoken ,oauthtokensecret;
{
  oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
    if (error) {
      console.log(error);
      res.send("yeah no. didn't work.")
    }
    else {
      oauthtoken = oauth_token;
      oauthtokensecret = oauth_token_secret;      
      open('http://twitter.com/oauth/authenticate?oauth_token='+oauthtoken);
    } 

  });
}

var http = require('http');
var fs = require('fs');
function readFile(fileName,response){
  fs.readFile(fileName, function (err,data) {
  if (err) {
    return console.log(err);
  }
  response.end(data);
});
}

var server = http.createServer(function(req, res) {

  if(req.url == '/favicon.ico'){
      res.end('Hello Http');
   }else if(req.url.indexOf('twitterCallback') != -1){

      res.writeHead(200);
      res.end('Hello Http');
      var url = require('url');
      var url_parts = url.parse(req.url, true);
      var query = url_parts.query;
      var oauth_verifier = query.oauth_verifier;
      console.log(oauth_verifier);
      oa.getOAuthAccessToken(oauthtoken,oauthtokensecret,query.oauth_verifier, function(error, oauth_access_token, oauth_access_token_secret, results)
        {
          if (error){
            console.log(error);
          } else {
            //console.log("oauth_access_token = " + oauth_access_token);
            //console.log("oauth_access_token_secret = " + oauth_access_token_secret);
          }
          var twitter = require('ntwitter');

          var twit = new twitter({
            consumer_key: 'NeFpQZNDapDtFJnUXDe0Q',
            consumer_secret: 'mXf7AKYJWIdiToKuSgsTm2me0GBA1aNn39uskmEoOM',
            access_token_key: oauth_access_token,
            access_token_secret: oauth_access_token_secret
          });

          twit.stream('statuses/sample', function(stream) {
            stream.on('data', function (data) {
              console.log(data.text);
            });
          });

        }

      );
    }else{
      readFile(req.url.substr(1));
    }
});
server.listen(8080);

1 个答案:

答案 0 :(得分:0)

使用ntwitter库:https://github.com/AvianFlu/ntwitter

消费api:

var twitter = require('ntwitter');

var twit = new twitter({
    consumer_key: 'Twitter',
    consumer_secret: 'API',
    access_token_key: 'keys',
    access_token_secret: 'go here'
});

twit.stream('user', {track:'nodejs'}, function(stream) {
    stream.on('data', function (data) {
        console.log(data);
    });
    stream.on('end', function (response) {
        // Handle a disconnection
    });
    stream.on('destroy', function (response) {
        // Handle a 'silent' disconnection from Twitter, no end/error event fired
    });
    // Disconnect stream after five seconds
    setTimeout(stream.destroy, 5000);
});