Update Your Linux Box Status to Twitter Using oAuth

Twitter recently announced (source) that they will be shutting down basic authentication on the Twitter API. So all applications, need to switch to using Oauth, which means the earlier article on Update Your Linux Box Status to Twitter will not work for the readers.

This article aims to fill up the gap by implementing oAuth using PHP . So before starting, lets check the perquisites for proper working of this article .

Twitter Linux

System Perquisites:

A Linux Box

PHP5 with php-curl ( php5-curl ) installed

A working Internet Connection

The first step involve in having a oAuth Twitter app is to register your application with twitter.

Steps for Registering Twitter Application :

  1. Visit http://dev.twitter.com
  2. Click on Register Your App Link
  3. Fill the Form with App Name, Description, Website link ( if you dont have any link put your profile link instead ), Organization
  4. Select Application Type: “Client”
  5. Select Default Access type: “Read & Write”
  6. Read/listen captcha and click on register application
  7. Accept the Terms and Conditions of Twitter API usage

Next step involve extracting information regarding your application which will be needed by PHP script. Here are steps for extracting information :

  1. visit http://dev.twitter.com/apps
  2. Select your application
  3. Copy/Note Down the following informationa) Consumer keyb) Consumer secret
  4. Now click on “My Access Token” link
  5. Copy/Note Down the following informationa) Access Token (oauth_token)b) Access Token Secret (oauth_token_secret)

Now users need to download the oAuth PHP library ( We have used @abraham’s Twitter OAuth library for PHP )

Download @abraham’s Twitter OAuth library for PHP from here

Extract twitteroauth/ directory to a specified folder

now create a tweet.php file with following code, in the specified folder

<?php
 
require_once('twitteroauth/twitteroauth.php');
 
$ckey = "Place_Your_Consumer_key_here";
$csecret = "Place_Your_Consumer_Secret_here";
 
$oauthtoken = "Place_Your_oauth_token_here";
$oauthtokensecret = "Place_Your_oauth_token_Secret_here ";
 
$message = exec('uptime'); //Command to be executed
 
$connection = new TwitterOAuth ($ckey ,$csecret , $ oauthtoken , $ oauthtokensecret );
 
$connection->post('statuses/update', array('status' => $message));
 
?>

Replace the variables above and save the file

Now test the script by passing following command

php /path/to/script/tweet.php

If everything is running fine, then users can copy paste the command in cronjob. After this, the machine will tweet its uptime regularly 🙂

Please let me know if you are facing any problem with this script by leaving your comment(s)

By | 2018-04-09T06:39:23+00:00 August 3rd, 2010|Command, Featured Post, Script, Tutorials, Twitter|8 Comments

About the Author: