- How to create a twitter application.
- How to get OAuth tokens.
- How to authorize users.
- How to authenticate users whenever he logs in.
- In Simple Words
How to create a Twitter Application?
- Go to http://dev.twitter.com
- Sign in with your twitter account.
- Go to https://dev.twitter.com/apps and click Create a new application button.
- The following page will load and you will fill the fields specified.
Name -> Name of your App.
Description -> Description of your app.
Website -> Site where this app will be available or where info of this app will be available.
Callback URL(Optional) -> This is the URL where the user will be redirected after he authorizes your app.
How to get OAuth tokens?
- After creating application you can see details of your app. There in OAuth Settings menu Consumer Key and Consumer Secret Key is available. Also know as Access key and Access Secret
Note down the both keys and save it. Never let anyone know your consumer key or secret except you, it may lead to misuse of your
How to Authorize user?
Download twitter OAuth library for PHP from following link and include in your code.
Create a TwitterOauth object using your consumer key and secret key.
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
Request Access tokens for the app. Specify your callback URL, if you left it blank while creating app.
$request_token = $twitteroauth->getRequestToken(YOUR_CALLBACK_URL);
- User logs in and approves your app and twitter returns with oauth_token and oauth_token_secret . Please be aware that these tokens are different from your consumer token and secret.
Authorize URL is called to authorize currently logged in user to use your application.
$url = $twitteroauth->getAuthorizeURL(oauth_token);
- After Authorization, twitter will send oauth_verifier code.
Now request for access tokens again using oauth_verifier
$access_token = $twitteroauth->getAccessToken(oauth_verifier);
- Now you will get permanent oauth_token and oauth_secret for the user. Which you can save in database and user need not authorize the app everytime.
How to Authenticate user whenever he logs in?
- Include twitter libraries in your code.
Get user oauth_token and oauth_secret which is stored in database and use following code to get twitter oauth object.
$connection= new TwitterOAuth(CONSUMER_KEYCONSUMER_SECRET, oauth_token, oauth_token_secret);
Using the $connection object now authenticate user using following code.
$auth=$connection->get(‘https://api.twitter.com/oauth/authenticate’, array(‘oauth_token’ => oauth_token));
- For more details please visit. https://dev.twitter.com/docs/api/1/get/oauth/authenticate
In Simple Words
- Your App requests User permission.
User approves (This is authorization)Using the $connection object now authenticate user using following code.
From the moment user approves your app, his account is accessible by your app without his login credentials or his permission. (This is authentication)
Your app can then do all the twitter work (tweet, retweet, follow, unfollow etc..) behalf of the user.