设置Twitter API,获取最后的推文

一般来说,我对使用Twitter是完全陌生的,从来没有在任何项目中embedded“最新的推文”。 我只是试图在站点页脚中embedded3-4个最新的推文,而没有附加的function特性。 我一直在研究如何做相当一段时间,并有一些麻烦。

我将下面的代码片段添加到项目中,工作得很好,但是,我不确定如何更新代码片段,以便使用我的Twitter帐户而不是其设置的代码。

<div id="twitter_update_list"> </div> <script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stackoverflow&include_rts=true&count=4&callback=twitterCallback2"> </script> 

此外,我一直在阅读,最常用的Twitter API将停止工作,因为Twitter要人们使用自己的,而不是第三方。

我不知道如何从这里开始。 在这方面,我将不胜感激。 回顾一下,我所要做的就是从我的帐户中获取最新的3-4条推文。

提前谢谢了!

所以你真的不想再做这个客户端了。 (刚刚经历了很多文档,开发人员build议做所有oAuth服务器端)

你需要做什么:

首先 :在https://dev.twitter.com上注册; ,然后创build一个新的应用程序。

第二 :注意:您的消费者密钥/秘密以及访问令牌/秘密

第三 :下载Twitter的oAuth库(在这种情况下,我使用了PHP库https://github.com/abraham/twitteroauth ,位于这里的其他库: https ://dev.twitter.com/docs/twitter-libraries)

第四 :(如果使用php)确保cURL被启用,如果你在LAMP上运行这里是你需要的命令:

 sudo apt-get install php5-curl 

第五 :创build一个新的PHP文件并插入以下内容:感谢Tom Elliot http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/

 <?php session_start(); require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library you downloaded in step 3 $twitteruser = "twitterusername"; //user name you want to reference $notweets = 30; //how many tweets you want to retrieve $consumerkey = "12345"; //Noted keys from step 2 $consumersecret = "123456789"; //Noted keys from step 2 $accesstoken = "123456789"; //Noted keys from step 2 $accesstokensecret = "12345"; //Noted keys from step 2 function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); return $connection; } $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); echo json_encode($tweets); echo $tweets; //testing remove for production ?> 

繁荣 ,你完成了。 我知道这不是一个纯粹的JS解决scheme,但再次阅读新的Twitter API 1.1文档,他们真的不希望你做这个客户端网站。 希望这有助于!

如何使用核心PHPfunction只有最后几个用户的鸣叫(没有CURL或Twitter的oAuth库需要):

  1. 注册您的应用程序/网页https://dev.twitter.com (您可能还需要validation您的个人帐号手机号码)

  2. 注意消费者密钥和消费者秘密

  3. PHP代码:

     // auth parameters $api_key = urlencode('REPLACEWITHAPPAPIKEY'); // Consumer Key (API Key) $api_secret = urlencode('REPLACEWITHAPPAPISECRET'); // Consumer Secret (API Secret) $auth_url = 'https://api.twitter.com/oauth2/token'; // what we want? $data_username = 'Independent'; // username $data_count = 10; // number of tweets $data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended'; // get api access token $api_credentials = base64_encode($api_key.':'.$api_secret); $auth_headers = 'Authorization: Basic '.$api_credentials."\r\n". 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8'."\r\n"; $auth_context = stream_context_create( array( 'http' => array( 'header' => $auth_headers, 'method' => 'POST', 'content'=> http_build_query(array('grant_type' => 'client_credentials', )), ) ) ); $auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true); $auth_token = $auth_response['access_token']; // get tweets $data_context = stream_context_create( array( 'http' => array( 'header' => 'Authorization: Bearer '.$auth_token."\r\n", ) ) ); $data = json_decode(file_get_contents($data_url.'&count='.$data_count.'&screen_name='.urlencode($data_username), 0, $data_context), true); // result - do what you want print('<pre>'); print_r($data); 

用XAMPP for Windows和Centos6默认安装(PHP 5.3)

最可能的问题可能是在php.ini中未启用openssl

要修复检查,如果扩展= p​​hp_openssl.dll或扩展= p​​hp_openssl.so行存在,并取消注释在php.ini

如果你需要一个只有javascript的解决scheme,你可以使用Jason Mayes的Twitter-Post-Fetcher http://jasonmayes.com/projects/twitterApi/

几天后,我会尝试一个新的工作,今天似乎是谁不能在服务器端工作的好方法。

实际上twitter有许多限制,因为像耐克和其他公司有很多比赛。 推特的阅读是有限的,因为如果你正在阅读最新的API,实际上是稍微落后一点。

他们也控制了DM的延迟,这意味着即使你这样做也不能即时DM,对方只会在X时间后才会收到。 如果你通过脚本来做,而且即使你尝试从一个单一的IP叽叽喳喳很多DM只会阻止你。