Anc*_*end 27 javascript twitter jquery twitter-oauth
我对使用Twitter一般都很陌生,并且从未在任何项目中嵌入"最新推文".我只是试图在网站页脚上嵌入3-4条最新的推文而没有其他功能.我一直在研究如何做这件事已经有一段时间了,并且遇到了一些麻烦.
我在项目中添加了以下代码片段,效果很好,但是,我不知道如何更新片段,因此它使用我的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>
Run Code Online (Sandbox Code Playgroud)
此外,我一直在阅读最常用的Twitter API将很快停止工作,因为Twitter希望人们使用他们自己的,而不是第三方.
我不知道如何从这里开始.我非常感谢这方面的任何建议.回顾一下,我所要做的就是从我的账户中获取最新的3-4条推文.
提前谢谢了!
Sta*_*boy 62
所以你真的不想再做这个客户端了.(刚刚通过大量的文档,开发人员建议做所有oAuth服务器端)
你需要做什么:
首先:在https://dev.twitter.com上注册,然后创建一个新的应用程序.
第二:注意:您的消费者密钥/秘密以及访问令牌/秘密
第三:下载Twitter oAuth库(在这种情况下,我使用了PHP库https://github.com/abraham/twitteroauth,其他库位于此处:https://dev.twitter.com/docs/twitter-libraries )
第四 :(如果使用php)确保cURL已启用,如果您在LAMP上运行,则需要以下命令:
sudo apt-get install php5-curl
Run Code Online (Sandbox Code Playgroud)
第五步:制作一个新的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
?>
Run Code Online (Sandbox Code Playgroud)
繁荣,你已经完成了.我知道这不是一个纯粹的js解决方案,但再次阅读新的Twitter API 1.1文档,他们真的不希望你做这个客户端网站.希望这有帮助!
Rau*_*nde 25
如何获取仅具有核心PHP功能的用户的最后几条推文(无需CURL或Twitter oAuth库):
注册您的应用程序/网页https://apps.twitter.com(您可能还需要验证您的个人帐户手机号码)
消费者密钥和消费者秘密
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);
Run Code Online (Sandbox Code Playgroud)使用XAMPP for Windows和Centos6进行测试默认安装(PHP 5.3)
最可能的问题可能是php.ini中没有启用openssl
要修复检查extension = php_openssl.dll或extension = php_openssl.so是否存在并且在php.ini中取消注释
| 归档时间: |
|
| 查看次数: |
66175 次 |
| 最近记录: |