Today i have worked on Twitter Oauth and faced many problems how to get access token for offline acces to my application etc.
In this tutorial i will show you how to authorize application and add tweets, remove tweets, follow friend and unfollow friend.
The script contains two folders called oauth and images with PHP files.
oauth
- OAuth.php // Twitter OAUTH library
images
callback.php //Call back page create permanent credentials
config.php // Configuration
connect.php // Check application credentials
destroysessions.php // Erase all old sessions
html.inc // html design view
index.php // Main index file show data
redirect.php // Redirect to twitter for authorization
Create App on twitter click here
PHP Code
Edit config.php
|
1 2 3 4 5 6 7 |
<?php /** * A single location to store configuration. */ define('CONSUMER_KEY', 'Consumer key'); define('CONSUMER_SECRET', 'Consumer secret'); define('OAUTH_CALLBACK', 'http://demo.phpgang.com/twitter_login_oauth/callback.php'); |
Index.php
Shows a button for twitter authorization if not authorized.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php /** * User has successfully authenticated with Twitter. Access tokens saved to session and DB. */ /* Load required lib files. */ session_start(); require_once('oauth/twitteroauth.php'); require_once('config.php'); if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) { header('Location: ./destroysessions.php'); } $access_token = $_SESSION['access_token']; $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); /* If method is set change API call made. Test is called by default. */ $content = $connection->get('account/verify_credentials'); /* Some example calls */ //$connection->get('users/show', array('screen_name' => 'PHPGang')); //$connection->post('statuses/update', array('status' => "PHP Gang Testing...")); //$connection->post('statuses/destroy', array('id' => 533297770)); //$connection->post('friendships/create', array('id' => 9322192)); //$connection->post('friendships/destroy', array('id' => 9436992)); include('html.inc'); |
Redirect you to redirect.php generate temporary credentials oauth access token and oauth token secret
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php /* Start session and load library. */ session_start(); require_once('oauth/twitteroauth.php'); require_once('config.php'); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);// Key and Sec $request_token = $connection->getRequestToken(OAUTH_CALLBACK);// Retrieve Temporary credentials. $_SESSION['oauth_token'] = $token = $request_token['oauth_token']; $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; switch ($connection->http_code) { case 200: $url = $connection->getAuthorizeURL($token); // Redirect to authorize page. header('Location: ' . $url); break; default: echo 'Could not connect to Twitter. Refresh the page or try again later.'; } |
Authorize on twitter and return on callback.php and get permanent credentials oauth access token and oauth token secret you can save them in database and update status any time you want.
Summery
When you authorize application it will return temporary credentials oauth access token, oauth token secret and a oauth verifier with verifier you will get new oauth access token and oauth token secret with this method.
|
1 |
$access_token = $connection->getAccessToken('oauth_verifier'); |
Now you will be able to get
User credentials
|
1 |
$content = $connection->get('account/verify_credentials'); |
Show user Information
|
1 |
$connection->get('users/show', array('screen_name' => 'PHPGang')); |
Post new tweet
|
1 |
$connection->post('statuses/update', array('status' => "PHP Gang Testing...")); |
Delete tweet
|
1 |
$connection->post('statuses/destroy', array('id' => 533297770)); |
Follow a friend
|
1 |
$connection->post('friendships/create', array('id' => 9322192)); |
Unfollow a friend
|
1 |
$connection->post('friendships/destroy', array('id' => 9436992)); |








this doesn’t gets email id of the twitter user like facebook. how can we get twitter users email id?
The user’s email address can not be retrieved via the API. This is a deliberate design decision by the API team.