Twitter has announced to shut down the Basic Authentication API on August 31st and recommend Twitter application developers to migrate their apps to OAuth Authentication. What about bots? That’s the same. If you have bot created using Basic Authentication, then you should migrate your bot to OAuth. Here’s how, but first..
What is OAuth?
OAuth is a new authentication method that doesn’t use plain username & password to authenticate. OAuth uses a pair of tokens to check your credentials, therefore you don’t need to worry about leaking your password. These tokens are unique per user and per application. For further info about OAuth, check http://oauth.net/
So how?
First thing you need to do after creating bot account is creating an application. Go to http://dev.twitter.com/apps/new with your bot account and fill the form.
– Name: the application name will be shown after “via” in your tweets
– Application Type: You must choose ‘Browser’
– Default Access Type: Choose ‘Read and Write’
– Everything else you can fill any value
After created, go to you application detail. There you can see ‘Consumer Key’ and ‘Consumer Secret’ value. Save these keys.
Then click ‘My Access Token’ button in the right. You will see your ‘Access Token’ and ‘Access Token Secret’. You should save these keys too.
And now let’s go to the code!
You’ll need some libraries to utilize OAuth functions, so download Abraham’s TwitterOAuth library first. Copy the OAuth.php & twitteroauth.php files from downloaded library to your server, and create a new PHP file in the same directory.
And here’s the code:
this is the code I use for my bot @juru_doa 😉
require_once('twitteroauth.php'); define('CONSUMER_KEY', 'insert_your_consumer_key_here'); define('CONSUMER_SECRET', 'insert_your_consumer_secret_here'); define('ACCESS_TOKEN', 'insert_your_access_token_here'); define('ACCESS_TOKEN_SECRET', 'insert_your_access_token_secret_here'); $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); $twitter->host = "http://search.twitter.com/"; $search = $twitter->get('search', array('q' => 'semoga', 'rpp' => 20)); $twitter->host = "https://api.twitter.com/1/"; foreach($search->results as $tweet) { $status = 'Amiiiin RT @'.$tweet->from_user.' '.$tweet->text; if(strlen($status) > 140) $status = substr($status, 0, 139); $twitter->post('statuses/update', array('status' => $status)); }
What above code does?
After authenticating the OAuth tokens, it will do a search for ‘semoga’ keyword and grab the 20 most recent tweets. You can set another keyword for your bot. Then it will ReTweet each tweets and and ‘Amiiin’ before it.
To try whether you bot works or not, you can access your script from a browser. If the displays blank page, then your script might be work. Go check the timeline to make sure.
The last step is creating a cron job to run your script in certain times. How? That part is what you should do yourself 😆
Tips: Don’t run your bot too often, like in every minutes, and don’t tweet too much, since there are limitation from Twitter.
Discuss about Twitter Bot here
190 replies on “How to Create Twitter Bot using OAuth”
Hmmmmm… *ngikik licik
LikeLike
OMG!
ternyata juru_doa tuh kamu yg bikin toh? =))
keplak2 nazib.. kmrn twit gue ada yg di RT hahaha
LikeLike
Alhamdulillah akhirnya ada juga kode yang bisa dipake 🙂 makasih mas jadi bisa buat bot. maklum gak bisa koding. dan baru ngeh ternyata 31 agustus basic auth twitter dimatiin ya? 🙂
LikeLike
Hii..
btw,
Then click ‘My Access Token’ button in the right. You will see your ‘Access Token’ and ‘Access Token Secret’. You should save these keys too.
itu di kanan sebelah mana?ko saya ngga nemu ya? 🙂 yg ada cuma consumer key+consumer secret?
LikeLike
Aha solved.
gw liat nya di http://twitter.com/apps
ngga baca, harusnya liatnya di http://dev.twitter.com/apps
thx.
LikeLike
dear …
great post guys ..
but can i put your script to set with cron jobs (i run with curl / CLI[command line interface] )
big thanks in advance
LikeLike
Sure, if you can run the script on the browser there will be no problem running it on CLI
LikeLike
Wahihi..
LikeLike
[…] This script is no longer working since Twitter changed its authentication method. You can try this script with OAuth authentication, written by […]
LikeLike
oalah! 😀
LikeLike
“Copy the OAuth.php & twitteroauth.php files from downloaded library to your server”
what the mean of server here?can u help me? i’m confused on that, but i wanna learn. however, i’m a newbie. :))
LikeLike
same, newbie 🙂
LikeLike
Kok ga jalan ya.. udah di echo biar yakin result nya keluar apa nggak, ternayta keluar.. tapi pas cek di time line ga ada update apa2.. pdhal ga ada warning ato error.. 😮
LikeLike
kalo boleh tau result-nya apa ya?
LikeLike
Genial fill someone in on and this mail helped me alot in my college assignement. Gratefulness you for your information.
LikeLike
does this script have a limitation only for ReTweet? how about if i wanna make a bot that can reply (not RT) or quote tweet. I want to make a bot that teach people not to abuse RT. So bot with RT isnt suitable for my bot.
the plan is not about RT-ing keyword’s search result like “semoga” you showed above but everytime someone RT with editing (RT abusing), my bot will come up and tell (e.g. by normal reply or quote tweet) not to use RT for replying tweets.
is it possible?
thx
LikeLike
waaahaha
ternyata sederhana yah…
patut dicoba
*pikiran nakal*
LikeLike
error mulu 😦
LikeLike
aminnn… wah banyak pahalanya nih..
btw, thanks for sharing yaa!
LikeLike
Hellow,
I have used this example for a twitterbot. I have make the pages so as in this example and everything on Twitter.com, but when i start the page i see a blank page and on twitter i do not see any of tweet what i have send.
Some one who can help us? first i wil start to send the basis info of this example before a shall rewrite them in the website.
LikeLike
Great,
what an info and tuts*
Thanks a bunch!
LikeLike
thanks a lot for spending the time to write it. I like the direction you are taking your blog.
LikeLike
ahihihi just search me on twitter :))
LikeLike
wahihi
LikeLike
Saya sudah bisa, lalu bagaimana cara mensetting cron job?
LikeLike
Ohya, saya sudah memasang ini di cron job cpanel:
/usr/bin/php -q/home/username/public_html/twitteroauth/retweetbot.php
Saya setting per 1 jam tidak bisa. Bagaimana bos?
LikeLike
can only be used one time only. 😦
LikeLike
aku dah nyoba untuk bandku, tapi belum ada reaksi di timeline, tetapi untuk nyobain skrip, sudah muncul halaman kosong.
LikeLike
status pas nge-post update status = 401 😦
LikeLike
How can I add more keywords to that script?
LikeLike
[…] In this instructional video, I display you how to develop a straightforward Twitter bot in five simple steps. Internet sites utilized: feedburner.google.com pipes.yahoo.com twitterfeed.com botize.com paper.li And right here is the bot I developed in the course of the video: twitter.com In this instructional video, I display you how to develop a straightforward Twitter bot in five sim…fullscreen="true"> […]
LikeLike
Hey, nice bot. But Twitter api limitations are bad for that 😉 I will build one without api 😉
LikeLike
:O
LikeLike
pakek bhs indo napa?
gk ngerti gua??????
LikeLike
Thanks so much for the tutorial! I’ve tried it and it works great. Now I just need to figure out *why* to use it. 🙂
LikeLike
terima kasih atas informasinya….langsung, nih…
LikeLike
Need someone to programme a twitter bot for me.
LikeLike
when you click on their videos you have to enter the site then to start the video you have to click on an ad rncreeefe but you never get to see the video????????
LikeLike
thenks for information, langsung dicoba nih….
LikeLike
[…] If you dont have the knowledge of installing the scripts you want on your website, we offer fast installation of CGI scripts. Read More… […]
LikeLike
wah boleh dicoba nih *devil smile* 🙂
LikeLike
makasih atas infonya…
LikeLike
mantep gan infonya….
LikeLike
BNC Development GuruNovember 1st, 2010Hi Dim,We’ll be releasing a viosern tomorrow that has some additional logging and also reports errors a little better. Please hold out for that and then give us some more feedback. Thanks.
LikeLike
thx ya gan infonya..
LikeLike
thx ya uda berbagi, langsung dicoba dulu.,….
LikeLike
wah bang nazieb ternyata yg bikin botnya y..pantes twitternya kq aneh kaya orangnya..haha
LikeLike
I never know about oAuth before.. So with oAuth we can make twitter automacally right??
LikeLike
wow kerenn mass jempoll dahh
LikeLike
mantap komandan… 🙂
LikeLike
kok musti pake bahasa inggris segala sih. rakyat indonesia kan jadi pada gk ngarti. hehe
LikeLike
Yahoo Answers are loved by Google and many of the Yahoo Answers are sitting on high ranking especially for some highly competitive keywords. You can’t even imagine what effect can Yahoo Answers provide to the traffic of your site. Don’t wait and learn more about this at http://yanswertraffic.com/
LikeLike
I just could not leavе yοur web sitе prior tο
suggesting that I eхtremely loveԁ
the uѕual іnformatіon a person provide
for your visitοrs? Is going tο be agаin cеаselesslу
in ordеr to inveѕtigate сross-checκ nеw pοstѕ
my ωeb blog :: http://Www.Cotomarketing.Com
LikeLike