设置 Twitter API,获取最近的几条推文 [英] Setting up Twitter API, getting the last few Tweets

查看:18
本文介绍了设置 Twitter API,获取最近的几条推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 Twitter 完全陌生,从未在任何项目中嵌入最新推文".我只是想在网站页脚中嵌入 3-4 条最新的推文,而没有其他功能特性.我一直在研究如何做到这一点已经有一段时间了,但遇到了一些麻烦.

我将以下代码片段添加到项目中,效果很好,但是,我不确定如何更新该片段,因此它使用我的 Twitter 帐户而不是它设置的帐户.

 

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

此外,我一直在读到,最常用的 Twitter API 将很快停止工作,因为 Twitter 希望人们使用自己的 API,而不是第三方.

我不知道如何从这里开始.我将不胜感激在这方面的任何建议.总结一下,我想要做的就是从我的帐户中获取 3-4 条最新的推文.

非常感谢!

解决方案

所以你真的不想再做这个客户端了.(刚刚浏览了大量文档,开发人员建议在服务器端进行所有 oAuth)

你需要做什么:

首先:在 https://dev.twitter.com 上注册, 并创建一个新的应用程序.

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

第三:下载 Twitter OAuth 库(在本例中,我使用了 PHP 库 https://github.com/abraham/twitteroauth ,其他库位于此处:https://dev.twitter.com/docs/twitter-libraries)

第四:(如果使用 PHP)如果您在 LAMP 上运行,请确保启用了 cURL,这是您需要的命令:

sudo apt-get install php5-curl

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

get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);回声 json_encode($tweets);回声 $tweets;//测试删除生产?>

轰隆隆,大​​功告成.我知道这不是纯 js 解决方案,但再次阅读新的 Twitter API 1.1 文档,他们真的不希望您在客户端执行此操作.希望这有帮助!

I am completely new to using Twitter in general and have never embedded "latest tweets" on any project. I am simply trying to embed the 3-4 newest tweets on the site footer with no additional features of functionality. I have been researching how to do this for quite some time now and having some trouble.

I added the following code snippet to the project, which works quite well, however, I am not sure how to update the snippet so it uses my Twitter account instead of the one it is set up with.

    <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>

In addition, I keep reading that the most commonly used Twitter API will stop working soon because Twitter wants people to use their own, as opposed to third party.

I am not sure how to proceed from here. I would greatly appreciate any suggestions in this regard. To recap, all I am trying to do is grab the 3-4 latest tweets from my account.

many thanks in advance!

解决方案

So you REALLY don't want to do this client-side anymore. (Just went through numerous docs, and devs suggest to do all oAuth server-side)

What you need to do:

First: sign up on https://dev.twitter.com, and make a new application.

Second: NOTE: Your Consumer Key / Secret along with Access Token / Secret

Third: Download Twitter OAuth Library (In this case I used the PHP Library https://github.com/abraham/twitteroauth , additional libraries located here: https://dev.twitter.com/docs/twitter-libraries)

Fourth: (If using PHP) Make sure cURL is enabled if your running on a LAMP here's the command you need:

sudo apt-get install php5-curl

Fifth: Make a new PHP file and insert the following: Thanks to 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   
?>

And boom, you're done. I know this isn't a pure js solution but again reading through the new Twitter API 1.1 docs they REALLY don't want you to do this client-side. Hope this helps!

这篇关于设置 Twitter API,获取最近的几条推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆