是否有可能获得用户的Twitter推文(不是他们自己的帖子,但是如果他们要查看他们关注的所有人,他们会看到的帖子)?
我使用了这里的代码:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
但我得到的只是我自己的Twitter提要,无论传递什么$ twitterid。
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);
}
答案 0 :(得分:1)
不幸的是,Twitter没有提供此功能。
请参阅home_timeline端点上的文档:https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
"返回由身份验证用户及其关注的用户发布的最新推文和转推的集合。"
此处也没有选项来指定user_id或screen_name,因为它使用经过身份验证的用户(即您)。
user_timeline虽然允许您指定用户,但仅适用于您自己的推文。
我相信Twitter一度支持这一点,因为他们在网站上有一个功能来查看其他用户'时间表,但此功能已被删除,似乎在旧的已弃用的v1 API中甚至没有API。
因此,唯一的方法是让每个用户验证您的应用,然后加载他们的时间线。
答案 1 :(得分:0)
虽然Twitter API不支持阅读任何其他人的推特提要,但仍然可以采用以下两个步骤:
1-获取用户(例如@abc)"关注"列出a.k.a' friends / list'。这将返回'用户'作为人员细节的对象,@ abc正在关注。我使用的示例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-阅读' user_timeline'使用每个用户'对象的screen_name或user_id等来自步骤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
有关详细信息,请分别访问以下链接:
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