Since the deactivation of the old API, retrieving a public twitter feed from the API isn’t as simple as it used to be. All requests must now be authenticated, which requires creating an app with twitter and a signing your requests.
A while ago I wrote a post detailing how to create a simple twitter cache using PHP to avoid your Twitter feed disappearing when the API gets overloaded (which seems to happen very frequently). After reading through Twitter’s forums trying to figure out the easiest way to authenticate my requests, it turns out that there’s a very simple way to get your twitter feed up and running again without having to use a bunch of authentication libraries.
First, you’ll need to register an app with twitter. Sign into dev.twitter.com using your Twitter login details, and create an app. You can call it whatever you want, and it only requires read-only access. Once this it done, note down your Consumer Key and Consumer Secret.
The next step is courtesy of Kieran Metcalfe over from the Twitter Forum. He documented this awesome process of obtaining a bearer token, which will allow us to retrieve the feed without having to use an oauth library.
Run this snippet once using your key/secret combo to obtain the bearer token, and note it down.
You can then use the following modified version of my twitter feed cache, replacing the bearer token with your own. Save this as a file somewhere in your project directory; this is the file you’ll call to retrieve the twitter feed. You can change the interval if you want the file to be cached for longer/shorter period of time.
Note: you’ll need to create a cache folder in the same directory as the script, and a twitter-cache file inside that in order to avoid first-run errors. The script should create the file automatically if it doesn’t exist (albeit returning a warning), but you’ll need to make sure the folder exists first by creating it manually.
And now you’re free to do whatever you like with the feed. For example, if you want to insert it into your page with some jQuery, you can use the following code:
If you’ve got any questions or problems, please don’t hesitate to let me know in the comments.
If this post has helped you out, feel free to consider throwing a small donation my way.
Comments are closed.
20 comments
Thanks for the article man! Still relevant as of March 2017! Just for reference, this is how i wrote the JS to output the text of the tweet into my document. If anyone has any better way than please feel free to share!
var element = document.getElementById(‘twitter-row’);
jQuery.getJSON(‘/twitter-feed.php’, function(data) {
jQuery.each(data, function(i) {
var jsonStr = (data[i].text);
jQuery(element).append(” + jsonStr + ”);
});
});
Good to know the code is still relevant! That JS looks pretty neat, can’t really see any way to improve on that 🙂
Just wanted to drop by for a second and say thanks for this. The Twitter OAuth was tripping me up SUPER hard. (even more so coming from Facebooks much easier Authentication)
hey man
I am using the script and it ran well the first time, but when I try to change the username the feed does not reload and it still pulls data from the old username. Does anyone have a solution?
I am sorry for this previous comment. I just noticed it works by setting the interval so that the file does not get cached
Yeah, you can do that, but I’d really recommend keeping the cache. In my experiences, the Twitter API is quite unreliable. If you want to get tweets for multiple usernames, you can pass the username as GET parameter, and include the username in the cache filename. That way you’ll have separate caches for each username. For example: https://gist.github.com/levymetal/a32ab0dd8c699e1e5415
Thanks for your reply….since we are pulling the different username profiles on every page load I had to set the cache low, but I do appreciate your reply. Will definitely use this for future purposes.
Thanks, worked like a charm!
No worries, glad to see this little piece of code is still helping people out!
Hello Christian ,
Thanks , A great work. I need some more help from you though .Can you please suggest me how could I get the feed from Bloomberg using this method. I have replaced the bearer token and username as Bloomberg. will this work? Please tell me where I am making the mistake.
Thanks
Bloomberg has a public feed, so it should work. Just to make sure, I gave it a shot with the screen_name as bloomberg and can verify that it works. You didn’t specify what error you’re getting – check the error message, and it will tell you where the problem is. Make sure you followed my steps carefully of registering an app at dev.twitter.com then obtaining a bearer token.
I tried with this url https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=RottenLA&count=5&include_rts=1
and get error {“errors”:[{“message”:”Bad Authentication data”,”code”:215}]}
Someone can help me? thank you.
You can’t simply call the API like that because the 1.1 API requires authentication. This is what my post is all about, did you read it or did you just scroll to the bottom and write a comment in an attempt to get someone to help you? If you’re willing to put some effort into understanding what I’ve written then my post describes one way to solve your problem.
yes, Thank you. I solved my problem.
Thanks so much for sharing this. Just implemented it on a client’s site to speed up the page requests 🙂
I’m glad it worked out for you! This is definitely a lot faster than calling the twitter API every page load.
Is there a way to keep fetching tweets from a bunch of users every time they post new tweets with jQuery or I’ll have to use OAuth since this is cached?
I haven’t looked into it, but you’ll probably want to check out the streaming api.