twitter

Twitter SMS script update

I updated my Update multiple twitter accounts via SMS from one phone script to use OAuth, and made a note about gathering OAuth credentials for plain-text storage at Getting twitter OAuth tokens & secrets.

(Most apps are a little more interactive than this one and would store the details in a database, but it seems overkill for this application!)

Getting twitter OAuth tokens & secrets

Tags:

My Update multiple twitter accounts via SMS from one phone script was recently updated to use OAuth (since twitter were about to pull the plug on basic auth). The OAuth tokens and secrets are kept in the file - but how do you get them?

Simples; a script linked to the application's callback URL that will allow you to log in as the users you want to use and then print the details.

Here's the script I used. As with the other script, it uses abraham's twitteroauth library. It doesn't save the information anywhere - just shows them on-screen.

<?php
 
define('CONSUMER_KEY', 'MYKEY');
define('CONSUMER_SECRET', 'MYSECRET);
 
session_start();
 
?>
<html>
	<head><title>Get credentials</title></head>
	<body>
		<h1>Grabbing your oauth token & secret, mwahaha.</h1>
<?php
 
require_once('twitteroauth.php');
 
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] === $_REQUEST['oauth_token'])
{
	/* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
 
	/* Request access tokens from twitter */
	$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
 
	/* Save the access tokens. Normally these would be saved in a database for future use. */
	$_SESSION['access_token'] = $access_token;
}
 
if (	empty($_SESSION['access_token'])
		|| empty($_SESSION['access_token']['oauth_token'])
		|| empty($_SESSION['access_token']['oauth_token_secret']))
{
	/* Build TwitterOAuth object with client credentials. */
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
 
	/* Get temporary credentials. */
	$request_token = $connection->getRequestToken();
 
	/* Save temporary credentials to session. */
	$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
	$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
 
	/* If last connection failed don't display authorization link. */
	switch ($connection->http_code) {
	  case 200:
		/* Build authorize URL and redirect user to Twitter. */
		$url = $connection->getAuthorizeURL($token);
		header('Location: ' . $url);
		break;
	  default:
		/* Show notification if something went wrong. */
		echo 'Could not connect to Twitter. Refresh the page or try again later.';
	}
 
} else {
 
	/* Get user access tokens out of the session. */
	$access_token = $_SESSION['access_token'];
 
	/* Create a TwitterOauth object with consumer/user tokens. */
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
 
	$info = $connection->get('account/verify_credentials');
 
	echo '<h2>Got details for "',$info->screen_name,'"</h2>';
	echo '<p>Token: ',$access_token['oauth_token'],'</p>';
	echo '<p>Secret: ',$access_token['oauth_token_secret'],'</p>';
 
}
?>
 
	</body>
</html>

Finally thought of a use for my twitter script!

Yes, it took some time, but as I was sat in Wetherspoon's last night, I realised something else I could hook in to my twitter script.

We have a budget system that lives on the web server - we visit a web site, add purchases or income, and at the end of the month we should be able to work out how much we've saved (or not). Having bought my dinner, I was reading Facebook on the phone, when I thought it'd be useful if I could update the budget system from my mobile.

Voila! I could text to twitter with a certain hash tag and my script could handle it! Brilliant!

Admittedly in this case it's not perfect - I can only add basic information - but now the budget system will display a "temporary purchase" and ask you to fill in the fine detail the next time you visit the site. So, any time I spend money, I can text the details before I forget, and finish it off later on.

I then thought about another system I always wanted to start again - I used to keep closer track of my mileage and petrol costs, and thus miles per gallon. It's tricky to remember to jot down the mileage every time - so maybe if I could update by text, it'd help? Well, I've set up a system to record the information now, so I'll have to wait and see if I actually use it.

Finally - for general reminders - I set up the tags e[mail], r[eminder] and n[ote] to email me the tweet, for perusal at a later date.

I also set the script up to log actions so I had some sort of idea what was going on, or what strings didn't work.

I've updated the Update multiple twitter accounts via SMS from one phone page with the new script.

Taking Twitter SMS further

Ok, so my Update multiple twitter accounts via SMS from one phone covered how to use a script to send tweets from one account to many others. Simple and effective.

What if you wanted to do more? What if you wanted to control more with your tweets?

It'd be very easy to hook the script into a facebook app to update your status, for example (though I think you can already set up facebook to update your status via SMS). Or perhaps you could have a special code to look for that would do something on your server? I can't think of anything I'd want to do on mine, but then it only hosts websites - if I had a server in my house hooked up to other devices, it could do anything...

Update multiple twitter accounts via SMS from one phone

Tags:

Ever wanted to update multiple twitter accounts from your phone, but you don't have a smartphone and have to rely on SMS? It is possible - see Update multiple twitter accounts via SMS from one phone.

Syndicate content