Compago knowledge

  • Increase font size
  • Default font size
  • Decrease font size
Home Manuali Utilità How to setup a Twitter application to post a tweet using OAuth

How to setup a Twitter application to post a tweet using OAuth

E-mail Stampa PDF

If you want a website that communicate directly with Twitter, sending an update status message on a Twitter account or retrieving all the information that you want from that account, you must use an authentication procedure (OAuth) to connect to Twitter.

You can do it directly with a library or using a plugin, but always you need to setup a Twitter application in order to have the four keys:

  1. Consumer key
  2. Consumer secret
  3. Access Token (OAuth_token)
  4. Access Token Secret (OAuth_token_secret)

Then the first step is to obtain access tokens to act on behalf of a user account.

The setup

The application control panel offers the ability to generate these OAuth access tokens for the owner of the application, but first you need to create one.

So, you can start by visiting the "My applications" page, either by navigating to, or hovering over your profile image in the top right hand corner of the site and selecting "My applications":

This page contains a list of the applications you have created, along with a button to create a new application. Select the application you wish to generate a token for, and click on its title:

In the "Settings" page tab setup the "Name", the "Description" and the "Web Site" name of your application. The "Callback URL" should be left blank. Sending a single tweet, a user won’t directly interact via a browser.

Now you must choose the type of access that your application need. If you need access to the account only for read the messages then you can leave "Read only", but if you need to send a status update or type a direct message then you must select "Read and Write" or "Read, Write and Access direct messages". then go to the "Settings" tab and select the Application type access:

In the "@Anywhere domain" page tab add the domain of your website:

At the bottom of the "Details" page tab, you will see two of the keys than you need: Consumer Key and Consumer Secret.

Now you need to create the last tokens.

In a section labeled "your access token", click on the "Create my access token" button, and an authorized access token and secret will be generated for your account and the current application. These values may be used to authorize requests to the Twitter API.

In this way you get all the four keys, and you can use them in the request for Twitter.

Twitter and PHP OAuth

To comunicate with Twitter by server side, we need is awesome Abraham Twitter OAuth library. You can download the archive file, where you will find a few files, but we will need only two: twitteroauth/twitteroauth.php and twitteroauth/OAuth.php.

As a test you can create a new file "test.php" where you need to include these library files:


Sample code in test.php:

require_once 'twitteroauth/twitteroauth.php';
$consumer_key       = 'sdgqgwgewggw';
$consumer_secret    = 'ffwaeft435345t4fewf3c';
$oauth_token        = 'dcqcq4r454352v4353245v';
$oauth_secret       = 'dfcr34r54c34rcfefergtht5y543535f3rfr'; 
//create a new connection to Twitter using the four keys
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_secret);
//set the parameters for the new status update request
$parameters = array('status'=>'this is my message');
$status = $connection->post('statuses/update',$parameters);

In this example we send to the twitter account a simple message (max 140 chars). As result you will get the "$status" object that can contain usefull information.

If you want to know how many tweets you can send:

//use the previous keys and connection code
$content = $connection->get('account/rate_limit_status');
echo "Current API hits remaining: {$content->remaining_hits}.";

If you need informations about the user:

$user = $connection->get('account/verify_credentials');
Ultimo aggiornamento ( Lunedì 02 Gennaio 2012 17:52 )