AllFacebook InsideFacebook InsideMobileApps InsideSocialGames SocialTimes LostRemote TVNewser TVSpy AgencySpy PRNewser MediaJobsDaily UnBeige

Getting Started with the Twitter API

What is exactly is the Twitter API and what can it do for me?

An API or Application Programming Interface is, simply put, a set of tools and functions to access another system’s internal computer systems. An API can be used to perform tasks, do searches of the target system’s data or post data to their systems among other things. If you have followed one of those pop-ups that start off with “An application would like to connect to your Twitter account…” then you have used the Twitter API.

Twitter’s API provides everything from simple features such as the Tweet Button which can be placed on any website to the full suite of searches, follow/unfollow and timeline functionality that Twitter client’s like Hootsuite and TweetDeck use to access your Twitter account. All of these use the same API to instruct Twitter to do your bidding.

Twitter offers a handful of ways to connect to and use their API. Some require no authentication and require zero programming, like Twitter Web Intents. On the other end of the spectrum, if you want to write your own code to tweet, search and whatever else you can imagine with Twitter, there are options for you too… keep reading.

Signup

First, we need to register a new application with their development site at dev.twitter.com. The application will tweet for the Twitter user that you use to signup for the development account. Be cautious here. Consider creating a new private Twitter account to play with as you are developing your application. You don’t want to be debugging your shiny new Twitter app in front of your 50,000 followers when it goes rogue and starts randomly retweeting your private @ParisHilton list… but that’s another story.

First off, you will need an a name for your application and a URL it will be installed on. The Application Name and Application Website that you submit will be displayed in your Twitter client under the tweet source (ie. web, Hootsuite, TweetDeck et al) and linked to the URL provided. These names are unique too, so don’t burn your company name on a test app.

Select Browser for Application Type.

Callback URL must be the website from which you will be executing your code. If you are creating an application to work with domain.com and tweet for @DomainsTweets then the Callback URL should be something at http://domain.com. If your application will require authorization from other user’s Twitter accounts then they will be redirected back to this URL after they have authorized your app for access. If you attempt to execute your Twitter app from a domain other than the domain of this URL you’ll probably get an authorization error.

If you want your app to tweet, select the non-default Read / Write option under Default Access Type.

Once you complete and submit the form, your application will be assigned an API key and various other security keys for authentication. On the first page, copy and paste out API key (which is the same as Consumer key below it) and Consumer secret, you will need these to connect to the API.

On the right-hand menu, select My Access Token and the next page will list two more tokens you will need to connect: Access Token and Access Token Secret. Copy and paste these out for later as well.

Connecting

The libraries section of the Twitter Developers site lists close to twenty languages and over sixty different public libraries that are available to connect to the Twitter API using your favorite web language such as PHP, Python, Ruby or Perl. It shouldn’t be too hard to find a library that works with your favorite scripting language.

My examples are written in PHP and use the twitter-async library listed at Twitter. Of the few I tested, this seemed to be the easiest to get up and running on the first try.

To follow the steps here, download the package above and unzip locally. Edit the file simpleTest.php to include the keys and tokens saved earlier from dev.twitter.com. Upload it and all of the other files in the downloaded directory to a private, password protected directory on your website. Anyone with access to the page will be able to generate tweets every time the page is accessed so take the time to lock it up.


<?php
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
$consumer_key = '';
$consumer_secret = '';
$token = '';
$secret= '';
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $token, $secret);
$twitterObjUnAuth = new EpiTwitter($consumer_key, $consumer_secret);
?>

Once you access the simpleTest.php page, it will generate a tweet from your account that says: This a simple test from twitter-async…

From here you should be able to play around with sending different types of tweets. Twitter’s API documentation does a great job of explaining and giving examples for each of the functions in their API, such as for the post tweet functionality (in Twitter dev speak status/update) simpleTest.php uses.

For other ideas, check out the /tests/ directory in the download. There are other examples of basic Twitter functionality in sequencerTest.php and EpiTwitterTest.php.

Next time we will take a look at doing something useful with these tools. Use the comments to shout out ideas of what type of mini-app or functional examples you’d like to see.

Mediabistro Course

Content Marketing 101

Content Marketing 101Get hands-on content marketing training in our brand new boot camp, Content Marketing 101! Starting September 8, digital marketing and content experts will teach you the tips and tricks for creating, distributing and measuring the success of your brand's content. Sign up before August 15 and get $50 OFF registration. Register now!