12,306 views

To continue with the previous Twitter Tutorial - Less Than a Minute Twitter Application Registration, I will now proceed and use our previously gathered consumer_key and consumer_secret to retrieve a single access token - i.e. access token and oauth_token_secret (collectively, an "access token"). We will do the retrieving of access token at this stage because the Twitter Application we are going to build is a "single-user use" and it don't need to implement the entire OAuth token acquisition dance once you have secured your access token some where in your website - hint - store this on a database together with your consumer_key and consumer_secret. A single user means - YOU - will be the only one who's going to use the application and this application will start using OAuth directly with an access token. This is not intended to be used for public therefore keep in mind that you should at all times secure the place where you would upload this application.

By Rnel, on September 05, 2010, under Twitter Tutorials
 

To continue with the previous Twitter Tutorial - Less Than a Minute Twitter Application Registration, I will now proceed and use our previously gathered consumer_key and consumer_secret to retrieve a single access token - i.e. access token and oauth_token_secret (collectively, an "access token").

We will do the retrieving of access token at this stage because the Twitter Application we are going to build is a "single-user use" and it don't need to implement the entire OAuth token acquisition dance once you have secured your access token some where in your website - hint - store this on a database together with your consumer_key and consumer_secret. A single user means - YOU - will be the only one who's going to use the application and this application will start using OAuth directly with an access token. This is not intended to be used for public therefore keep in mind that you should at all times secure the place where you would upload this application.

If you want to know about the "Sign in with Twitter" like Twitter Applications, it is when you would like your website visitors sign in with Twitter from your website, I will go through that in the future after this Twitter Tutorial has been completed. - hint - Those kind of Twitter Applications need to go through the entire OAuth token acquisition dance

Prepare your consumer_token and consumer_secret as you will be needing them when you make the retrieval of an access token of your Twitter Application in a while.

On this process you are now going to retrieve your access token. You need an access token to proceed and make a request to Twitter, a sample of request to Twitter that you could make is having a user from your website send "tweets" using your Twitter Application, on that manner your Twitter Application as the consumer will make a request to Twitter i.e. to send "tweet" on your user's behalf. However we will not talk about that because in my case, I need to build a Twitter Application that I am the only one who should use and that I could send "tweets" from My website's Control Management System - CMS - and that is what you will learn here.

What I did here is I created a Graphical user interface - GUI application where in you would just have to key in your consumer_token and consumer_secret and press continue button to make the access token retrieval happen. Although you really don't need to edit a single line from the scripts, you will still have an idea how it works at the backend as I will still discuss about the important parts of the class.

I've used a very easy to follow PHP library - tmhOAuth of Matt Harris - An OAuth 1.0A library written in PHP, specifically for use with the Twitter API. Check it out at github LINK. Follow him on twitter @themattharris

  • Needed Files: (all these are included on the available download file provided below)
  • - tmhOAuth.php - OAuth PHP library we are going to use
  • - RNELdotNETtokenExchange_tmhOAuth.php - class file I created using the tmhOAuth library to perform the task required
  • - index.php - main file where all actions happen

Extract and upload the files on a secured directory of your server.

  • You should have the following files:
  • - index.php
  • - tmhOAuth.php - under tmhOAuth folder
  • - LICENSE - under tmhOAuth folder
  • - README - under tmhOAuth folder
  • - RNELdotNETtokenExchange_tmhOAuth.php - under RNELdotNETtokenExchange_tmhOAuth folder

Browse index.php file using any web browser and continue reading below.

 

UPDATE: 01 Dec 2010

 

You can now retrieve your access tokens on your Twitter Application Page. Here's how.

 

Step 1:

Login to twitter developer application page - http://dev.twitter.com/login

 

Step 2:

Find and click the button "View Your Applications" or go directly to this page - http://dev.twitter.com/apps

 

Step 3:

Select your Application

 

Step 4:

Click on "My Access Tokens"

 

Step 5:

You now have your Access Tokens. Remember to secure them always.

 

UPDATE: 03 Oct 2010

 

Please take note that the files related to this Twitter Tutorial has been updated.

I am following the development of the tmhOAuth OAuth 1.0A library of Matt Harris and there are some new available developments since I've posted this twitter tutorial about a month ago. I've updated all related files to tmhOAuth PHP library and as a result, I also updated the files provided on this How to retrieve a single access token in Twitter with tmhOAut tutorial. I will list down below the developments from the tmhOAuth class and at the end provide the latest files for this twitter tutorial. A link to the main development tmhOAuth page will be also listed below. You will notice a small difference from the screen shots presented here from the actual PHP files because the files were developed and some codes needs to be re-written. But that won't confuse you in any way as long as you read and understand the tutorial carefully. Post any concerns you have about this Twitter Tutorial at comment form located the bottom on this page and I will try to help you retrieve your twitter single access tokens.

  • tmhOAuth Change History since my first post about this tutorial LINK
  • 28 September 2010 - Moved entities rendering into the library
  • 17 September 2010 - Added support for the Streaming API
  • 17 September 2010 - Fixed authorisation header for use with OAuth Echo
  • 17 September 2010 - Added use_ssl configuration parameter
  • 17 September 2010 - Moved curl options to config
  • 17 September 2010 - Fixed a bug in the GET requests
 
 
 

Overview: Initialization of the script

 

This block of code initializes the sesion, tmhOAuth class and RNELdotNETtokenExchange_tmhOAuth class. These are all needed for the execution of the script later.

 

	    	<php
			session_start(); //start the session

			//include tmhOAuth PHP Library
			include_once('tmhOAuth/tmhOAuth.php');
			
			//include RNELdotNETretrieveToken_tmhOAuth class
			include_once('RNELdotNETretrieveToken_tmhOAuth/RNELdotNETretrieveToken_tmhOAuth.php');
			
			$tmhOAuth = new tmhOAuth(array());
			$RNELdotNETretrieveToken_tmhOAuth = new RNELdotNETretrieveToken_tmhOAuth;
			?>
		

 

Step 1: consumer_key and consumer_secret Form

 

Front End

Enter your consumer_key and consumer_secret

 
 

Back End

This block of code is the "default" code. It will be the first thing that will be executed and the first thing you will see when you first browse index.php

This code has a form that has a consumer_key and consumer_secret text fields, you need to fill up those fields to proceed with the access token retrieval from Twitter. This is the first step of the short the dance we will perform.

 

		<?php
			if(!isset($_REQUEST['oauth_verifier'])){ //check if oauth_verifier is not set
			//load the "connect form" if the condition above is true
		?>
		
Enter you consumer_key
Enter you consumer_secret
<php } ?>

 

Step 2: initializeRequest for a request_token and proceed with Authorization if successful. Stop and display an error if not.

 

Front End

This is how it looks when your request has failed. Be sure you enter the correct consumer_key - consumer_secret combination.

 
 

This is how it looks when your request is successful. The name of your Twitter Application will be in the place where you can read RNELdotNET.

Sign in with your Twitter Account.

 
 

Back End

The next block of code will execute when $_REQUEST['oauth_connect'] is set. It will call the function initializeRequest from RNELdotNETtokenExchange_tmhOAuth class.

RNELdotNETtokenExchange_tmhOAuth::initializeRequest simply makes a request_token to Twitter via tmhOAuth library, it will then proceed with the authorization if the response status code from Twitter is equal to 200. HTTP Status Code 200 means the request has succeeded. More info about HTTP Status codes here.

Most common error here is that the consumer_key and consumer_secret sent to Twitter for a request_token is incorrect, so make sure you have the correct values of your consumer_key and consumer_secret of your Twitter Application.

 

	    	<php
			if(isset($_REQUEST['oauth_connect'])){ //check if "oauth_connect" is set
				//call initializeRequest from RNELdotNETretrieveToken_tmhOAuth class if the condition above is true
				$RNELdotNETretrieveToken_tmhOAuth->initializeRequest($tmhOAuth);
			}
			?>
		

 

Step 3: Authorization was successfull, secure oauth_token and oauth_token_secret

 

Front End

You will see a quick notice like this after you signed in.

 
 

You are now back on the index page with the access token from Twitter.

You need to secure your access token and never ever share the combination of your OAuth consumer key, consumer secret, oauth token, and oauth token secret.

 
 

Back End

Following the previous block of code will check if $_REQUEST['oauth_verifier'] is set. Call fetchResponse from RNELdotNETtokenExchange_tmhOAuth if true.

If the authorization from Twitter is obtained, Twitter will send you back to where you set the callback value when you make the request_token.

When you've reached this part of the script, it will display your needed oauth_token and oauth_token_secret values returned from Twitter after the successfull authorization. Marking the completion of the script since by this time you already have the oauth_token and oauth_secret values necessary for your Twitter Application.

 

	    	<php
			if(isset($_REQUEST['oauth_verifier'])){ //check if "oauth_verifier" is set
				//call fetchResponse from RNELdotNETretrieveToken_tmhOAuth class if the condition above is true
				$RNELdotNETretrieveToken_tmhOAuth->fetchResponse($tmhOAuth);
			}
			?>
		

 

Save a copy of your oauth_token and oauth_token_secret some where safe and secured, put it together with your previously acquired consumer_token and consumer_secret. You will be needing all of those values later on for your Twitter Application.

When you have all those tokens secured, you can keep the tmhOAuth class for later usage - but you need to delete all the files related to RNELdotNETtokenExchange_tmhOAuth class including the index.php file, you don't need those file anymore.

 

WHAT TO DO NEXT?

"Now that I've secured my Twitter Application's consumer_token, consumer_secret, oauth_token, and oauth_token_secret, can I start building my First Single-User Twitter Application?"

YES! With all tokens at hand you can build your First Single-User Twitter Application.

 

TO DO

Build your First Twitter Application.

Soon I will let you know how I've build my First Twitter Application. "RNEL Tweets From RNELdotNET CMS"