简体   繁体   中英

Getting Twitter home_timeline

Is it possible to get a user's twitter feed (not their own posts, but the posts they would see if they were to view all the people they follow)?

I used the code from here:

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

But all I get is my own twitter feed, no matter what $twitterid is passed in.

function getFeed($twitterid)
{
$url = "https://api.twitter.com/1.1/statuses/home_timeline.json";

$oauth_access_token = "XXXX";
$oauth_access_token_secret = "XXXX";
$consumer_key = "XXX";
$consumer_secret = "XXX";

$oauth = array( 'screen_name' => $twitterid,
                'count' => 3,
                'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0');

$base_info = $this->buildBaseString($url, 'GET', $oauth);
$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

// Make requests
$header = array($this->buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url . '?screen_name='. $twitterid.'&count=3',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

return json_decode($json);

}

Unfortunately, Twitter don't offer this functionality.

See the docs on the home_timeline endpoint: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline

"Returns a collection of the most recent Tweets and retweets posted by the authenticating user and the users they follow."

There is also no option here to specify a user_id or screen_name, as it uses the authenticated user (ie you).

The user_timeline, while letting you specify a user, is only for your own tweets.

I believe Twitter supported this at one point as they had a feature on the site to view other users' timelines, but this feature was removed, and it seems there wasn't even an API for it in the old deprecated v1 API.

So, the only way to do it would be for each user to authenticate your app, then load their timeline.

Although Twitter API doesn't support to read any other person's twitter feed, still a possible approach would be to follow the following two steps:

1- Get a user's (eg @abc) "Following" list aka 'friends/list'. This will return a list of 'user' objects which are the details of the persons, @abc is following. Sample Ruby code that I used is given below.

baseurl = "https://api.twitter.com"
path    = "/1.1/friends/list.json"
query   = URI.encode_www_form("cursor"=>"-1", "screen_name"=>"abc")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

http             = Net::HTTP.new address.host, address.port
http.use_ssl     = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
consumer_key     = OAuth::Consumer.new( your_consumer_key , your_consumer_secret )
access_token     = OAuth::Token.new(your_access_token , your_access_token_secret)

 # Issue the request.
request.oauth! http, consumer_key, access_token
http.start
response       = http.request request
following_list = JSON.parse(response.body)

 following_list["users"].each do |friend|
    read_timeline(friend["screen_name"])
 end

2- Read the 'user_timeline' using each 'user' object's screen_name or user_id etc. from the list obtained in step 1.

def read_timeline(friends_screen_name)

  baseurl = "https://api.twitter.com"
  path    = "/1.1/statuses/user_timeline.json"
  query   = URI.encode_www_form("screen_name" => friends_screen_name, "count" => 3,)
  address = URI("#{baseurl}#{path}?#{query}")
  request = Net::HTTP::Get.new address.request_uri

   # Set up HTTP.
  http             = Net::HTTP.new address.host, address.port
  http.use_ssl     = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  consumer_key     = OAuth::Consumer.new( your_consumer_key , your_consumer_secret )
  access_token     = OAuth::Token.new(your_access_token , your_access_token_secret)

   # Issue the request.
  request.oauth! http, consumer_key, access_token
  http.start
  response = http.request request

   # Parse and print the Tweet if the response code was 200
  if response.code == '200' then
    tweets = JSON.parse(response.body)
      tweets.each do |tweet|
        puts tweet["user"]["name"] + ":\t" + tweet["text"]
      end 
  end
end

For further details, visit the following links respectively:

1- https://dev.twitter.com/docs/api/1.1/get/friends/list

2- https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM