| Mobile | RSS

Update Your Linux Box Status to Twitter Using oAuth

August 3rd, 2010 | 7 Comments | Posted in Command, Featured Post, Script, Tutorials, Twitter

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)

About

Kunal Gautam a 23 Year Old(Young actually :P) geek and blogger from Pune, Maharastra, India.
Leave a Reply 6051 views, 10 so far today |
  • No Related Post
Follow Discussion

7 Responses to “Update Your Linux Box Status to Twitter Using oAuth”

  1. Markus Says:

    Thanks! Finally my Nagios Notifications work again, after Twitter disabled oAuth!

  2. Markus Says:

    I mean they disabled Basic-Auth. Sorry for that. But thanks again ;)

  3. Kunal Gautam Says:

    Cool :) Glad to know it helped you :)

  4. Chemicalph Says:

    I have this error
     
    PHP Fatal error:  Call to undefined function json_decode() in /root/Desktop/abraham-twitteroauth-76446fa/twitteroauth/twitteroauth.php on line 159
     
    Any suggestions man? :(

  5. Kunal Gautam Says:

    I think you need to install PHP XML-RPC package

  6. Chemicalph Says:

    Already installed still the same error! I found this!
    http://blog.silviasoft.com/2009/11/19/fatal-error-call-to-undefined-function-json_decode-php-version/
     
    I’m not sure where is the generate.php I’m a php noob, also I want to try this for Nagios notifications coz I  haven’t found a method that works with th new OAuth api update :yet (

  7. Kunal Gautam Says:

    hey if you don’t mind can you mail me whole script ( after removing your consumer key and secrets ) so that I can check where the problem is :) mail me at kunalgautam.mjh     at     gmail dot com :)

Leave a Reply