简体   繁体   English

获取Twitter home_timeline

[英]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)? 是否可以获取用户的Twitter提要(不是他们自己的帖子,而是他们将看到的所有帖子,如果他们查看所有关注的人)?

I used the code from here: 我从这里使用了代码:

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1 使用Twitter API版本1.1检索user_timeline的最简单的PHP示例

But all I get is my own twitter feed, no matter what $twitterid is passed in. 但是无论传入的是什么$ twitterid,我得到的都是我自己的twitter提要。

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. 不幸的是,Twitter不提供此功能。

See the docs on the home_timeline endpoint: https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline 请参阅home_timeline端点上的文档: 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). 这里也没有选项指定user_id或screen_name,因为它使用经过身份验证的用户(即您)。

The user_timeline, while letting you specify a user, is only for your own tweets. 尽管允许您指定用户,但user_timeline仅用于您自己的推文。

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. 我相信Twitter一度支持此功能,因为他们在网站上具有查看其他用户时间表的功能,但是此功能已被删除,并且似乎旧的v1 API中甚至没有针对该功能的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: 尽管Twitter API不支持读取任何其他人的Twitter feed,但是仍然可能的方法是遵循以下两个步骤:

1- Get a user's (eg @abc) "Following" list aka 'friends/list'. 1-获取用户(例如@abc)的“关注”列表,也称为“朋友/列表”。 This will return a list of 'user' objects which are the details of the persons, @abc is following. 这将返回“用户”对象的列表,这些对象是人员的详细信息,@ abc在后面。 Sample Ruby code that I used is given below. 下面提供了我使用的示例Ruby代码。

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. 2-从步骤1中获得的列表中,使用每个“用户”对象的screen_name或user_id等读取“ user_timeline”。

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 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 2- https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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