所以你真的不想再做这个客户端了。 (刚刚浏览了大量文档,开发人员建议在服务器端进行所有 oAuth 操作)
你需要做什么:
First: 注册https://dev.twitter.com,并提出新的申请。
Second:注意:您的消费者密钥/秘密以及访问令牌/秘密
Third:下载 Twitter OAuth 库(在本例中我使用了 PHP 库https://github.com/abraham/twitteroauth,其他库位于此处:https://dev.twitter.com/docs/twitter-libraries)
Fourth:(如果使用 PHP)如果您在 LAMP 上运行,请确保启用 cURL,这是您需要的命令:
sudo apt-get install php5-curl
Fifth:创建一个新的 PHP 文件并插入以下内容: 感谢 Tom Elliothttp://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
?>
And boom, 你完成了。我知道这不是一个纯粹的 js 解决方案,但再次阅读新的 Twitter API 1.1 文档,他们真的不希望您在客户端执行此操作。希望这个 helps!