Twitter OAuth the easy way – simple post to twitter script

After Twitter introduced mandatory authentication with OAuth, many of the current scripts for posting content to Twitter don’t work anymore.

OAuth can be great for more advanced authentication, but for a simple post to twitter script, it seems like a little overkill.

In this post you’ll learn how to create a simple script that uses a quick and dirty version of OAuth for posting new tweets to Twitter.

How to create a simple script

Simplified, Twitter OAuth involves sending both application tokens and user tokens back and forth between your site and Twitter.

If you want to authenticate multiple users, you need a full OAuth implementation, but if you only want a script that sends tweets from your site or application, the good news is that all the authentication tokens and keys can be reused, which makes it possible to build a very simple script, as long as you just get the required tokens and keys once .

To get the required key and tokens you need to carefully follow the next steps

Step 1 – Register your application

First you need to register your application at Twitter here.

Please notice that you need to log in to Twitter at the start of the Application  registration process. The account that you’re logging in to, is naturally also the twitter account that you’re application can post tweets to.

Signup form for twitter applications

Filling in the form is pretty straightforward.

The only special requirement for our purpose is set Default Access type to “Read & write”, so your application is allowed to post tweets to twitter

Step 2 – Consumer secret and Consumer key

When your application is registered by Twitter, you also have all the required keys for your script.

You find the consumer keys here:

View Your Applications -> Edit Details

The consumer keys can be found at the last part of the page

Step 3 – Access token and Access token secret

You find the Access token and Access token secret by clicking on the “My Access token” link in the right menu.

When you have the four keys/tokens:

  • Consumer secret
  • Consumer key
  • Access token
  • Access token secret

You’re ready to proceed

Step 4 – Twitter OAuth class

To connect to twitter using OAuth we’ll be using the brilliant Abraham Twitter OAuth class. The Abraham twitter OAuth class has a lot of overhead enabling more advanced authentication than is required for this script. You only need the two files “OAuth.php”  and “twitteroauth.php”.

You can download the two files directly here.

When you have downloaded the files, and uploaded them on your server, you’re ready for the last step

Step 5 – Post to twitter script

Having the required access tokens, keys and the Twitter OAuth class, it’s a piece of cake to build a script that posts messages to twitter.

You just need to insert the required keys and the path to the twitteroauth.php file in the script below, and then you’re up and running.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
<?php
$consumerKey = '<insert your consumer key';
$consumerSecret = '<insert your consumer secret>';
$oAuthToken = '<insert your access token>';
$oAuthSecret = '<insert your token secret>';
require_once($_SERVER['DOCUMENT_ROOT'].'/<insert path to twitteroauth>/twitteroauth.php');
// create a new instance
$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//send a tweet
$tweet->post('statuses/update', array('status' => 'Hello World'));
?>

Conclusion

The process for getting this script up and running might be a little  complex, but you only have to go through the process once, and then you have a very simple to use script for sending tweets to twitter from a PHP script.

Addition:

Combining with bit.ly to shortened your link and limit your entry

function get_short_link($url) {
$bitly_login="<bit.ly login>";
$bitly_apikey="<bit.ly api key";
$api_call = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$bitly_login."&apiKey=".$bitly_apikey);
$bitlyinfo=json_decode(utf8_encode($api_call),true);
if ($bitlyinfo['errorCode']==0) {
return $bitlyinfo['results'][urldecode($url)]['shortUrl'];
} else {
return false;
}
}
// reduce text to twitter format
$length = strlen($text);
$ch = 70; //if the text is above 70 characters, the message is shortened
if ($length >= $ch) {
$foo1 = (substr("$text", 0, $ch));
$foo = "$foo1...";
} else {
$foo = $text;
}

Quoted from:

tips4php.net

http://tips4php.net/2010/12/twitter-oauth-the-easy-way-simple-post-to-twitter-script/

http://bit.ly

Advertisements

Twitter to Facebook; Facebook to Twitter; My way :)

Ever wonder posting your comments on your twitter account to be automatically published on your facebook profile page or pages that you manage and vice versa posting at your facebook page to be posted too at your twitter account, keep reading to find out how 🙂

From articles we read on: mashable.com

1. Auto-Update with a Facebook Application


twitter-auto-update

If you’re looking to automatically post your Twitter updates straight to Facebook, the Twitter application (by Twitter) is your best bet. It’s pretty darn simple to use as well: just install the application and then click to allow Twitter to update your Facebook status. The Tweeterapp is of the same variety and includes a few additional features related to viewing and engaging with tweets while using Facebook, though there are some known issues.

Those of us who are loquacious Twitterers might opt instead for the Selective Twitter Status application, which will only update your wall with tweets that include the #fb hashtag. TweetSync provides a similar function, though it’s keyword based, and also includes an option to sync all your tweets unless a particular keyword is included in a tweet.

Facebook Page admins have a few additional options. One such option is Involver, which is actually a suite of Facebook applications for Pages. The Twitter for Pages app essentially just adds a Twitter tab with a collection of tweets to a Facebook Page. It’s a nice alternative for brands and companies who want to showcase their Twitter presence without over-updating their Page’s wall with an influx of tweets.

Or if you want to be a bit selective on the posting you want to carry over to FB, you may want to use Selective Tweets instead. I myself like this one, by the way. (apps.facebook.com)

On the other hand, to get the posting from FB to Twitter, I use that recommended at: www.facebook.com that is http://www.facebook.com/twitter to achieve my goal.  However, please do note that twitter has the limitation of 140 characters maximum to be posted as a tweet. 🙂

by Michael Gummelt on Friday, August 21, 2009 at 8:34am
Many people have asked us to make Facebook and Twitter work better together for those times when they want to share their content as widely as possible. We agree. Over the next few days, we will be releasing a feature that allows administrators of Facebook Pages to publish their Facebook updates to their Twitter accounts automatically. This will only link Facebook Pages to Twitter, not your individual profile.Public figures, musicians, businesses and organizations of all types who’ve created Facebook Pages often want to share a status update, a photo or an event with as many of their supporters as possible. Celebrities may want to share personal news or charities may want to put out calls for help to both their Facebook fans and their Twitter followers, all at the same time.

If you manage a Facebook Page, you now will be able to decide whether to share updates with their Twitter followers, and you also will be able to control what type of updates to share: status updates, links, photos, notes, events or all of them. If you have multiple Pages, you will have the option to link each of those Pages to different Twitter accounts. This new feature will soon be available at http://www.facebook.com/twitter.

A number of celebrities and organizations on Facebook are already using this feature to publish the content on their Facebook Page to Twitter and reach a wider audience. They include Dane CookLIVESTRONGThe World Wildlife Fund, and the NBAWNBA and D-League.

We are always looking to make it easy for you to use Facebook with your favorite websites and applications. Facebook Connect allows you to bring your Facebook profile with you across the Web. We recently worked with Yahoo to integrate your Facebook experience into Yahoo’s new homepage; we’ve built our own Firefox and iPhone applications in-house; and we built tools to help you bring your content from YouTube, Hulu and other sites into your Facebook profile.

Twitter was a natural next step to link with Facebook Pages because it is a powerful tool for broadcasting short messages widely.

I was able to build this new feature from start to finish as a summer engineering intern. Next month I’ll head back to school, but I’m excited to see my work here live on through all of the people who now will both share on Facebook and tweet on Twitter right from their Facebook Pages.

Let us know if there are other websites you’d like to work better with Facebook. Who knows? Maybe I’ll be able to build it when I return next summer.

Michael is getting ready to return to Stanford University for his junior year.

Thanks!