2010/09/01

Surviving the Twitpocalypse

The 2010 Twitpocalypse has come. Buffy didn't save us this time, I guess. And what that means is that Twitter is no longer doing basic authentication for anything but their home page. So, if you have code like this:
my $twit = Net::Twitter->new(
    username => 'varlo',
    password => 'ISoLoveEdward' , #No Jacob
    clientname => $client ,
    ) ;
You're kinda screwed.

It's using OAuth now. I don't fully grok OAuth, so if you want the full deal, you should Google it and all, but in a nutshell, there is a key and secret combination that uniquely identifies my application. You use this application and it says "I don't see a user key" and sends you to Twitter. You get a user key and value, and you can use that user key and secret in conjunction with the app key and secret until you decide to go to Twitter and disassociate your user key with the app key.

This means that at no point does the application have your password, the same password that you might use for your pizza delivery, bank, email, etc. Until I began to understand OAuth, I thought it was a PITA, but I now think it's at least 70% win.

I have discussed Net::Twitter before, so most of the following code has been up before. My preferred way of handling a status is to join STDIN or ARGV with a space, but there should be enough here for you to adapt it to your workflow.

#!/usr/bin/perl

# largely taken verbatim from
# http://search.cpan.org/dist/Net-Twitter/lib/Net/Twitter/Role/OAuth.pm

# Next step is to get the keys and secrets to a config.

use 5.010 ;
use strict ;
use IO::Interactive qw{ interactive } ;
use Net::Twitter ;
use Carp ;

my $status = join ' ', @ARGV ;
if ( length $status < 1 ) {
    while (  ) {
        $status .= $_ ;
        }
    chomp $status ;
    }

if ( length $status > 140 ) {
    say { interactive } 'Too long' ;
    say { interactive } length $status ;
    exit ;
    }
if ( length $status < 1 ) {
    say { interactive } 'No content' ;
    say { interactive } length $status ;
    exit ;
    }

say $status ;

# GET key and secret from http://twitter.com/apps
my $twit = Net::Twitter->new( traits          => [ 'API::REST', 'OAuth' ],
                              consumer_key    => 'GetYorConsumerKeyFromTwitterDotComSlashApps',
                              consumer_secret => 'GetYorConsumerSecretFromTwitterDotComSlashApps',
                              ) ;

# You'll save the token and secret in cookie, config file or session database
my ( $access_token, $access_token_secret ) ;
( $access_token, $access_token_secret ) = restore_tokens() ;

if ( $access_token && $access_token_secret ) {
    $twit->access_token( $access_token ) ;
    $twit->access_token_secret( $access_token_secret ) ;
    }

unless ( $twit->authorized ) {

    # You have no auth token
    # go to the auth website.
    # they'll ask you if you wanna do this, then give you a PIN
    # input it here and it'll register you.
    # then save your token vals.

    say "Authorize this app at ", $twit->get_authorization_url, ' and enter the PIN#' ;
    my $pin =  ;    # wait for input
    chomp $pin ;
    my ( $access_token, $access_token_secret, $user_id, $screen_name ) =
      $twit->request_access_token( verifier => $pin ) ;
    save_tokens( $access_token, $access_token_secret ) ;    # if necessary
    }

if ( $twit->update( $status ) ) {
    say { interactive } 'OK' ;
    }
else {
    say { interactive } 'FAIL' ;
    }

#========= ========= ========= ========= ========= ========= =========

# Docs-suggested
sub restore_tokens {
    my $access_token        = 'GetYrOwnAccessToken' ;
    my $access_token_secret = 'GetYrOwnAccessTokenSecret' ;
    return $access_token, $access_token_secret ;
    }

sub save_tokens {
    my ( $access_token, $access_token_secret ) = @_ ;
    say 'access_token: ' . $access_token ;
    say 'access_token_secret: ' . $access_token_secret ;
    return 1 ;
    }
And if this has helped anyone drive their pet dingo and XB Falcon into the post-twitpocalyptic wasteland, remember, I'm just here for the gasoline.
Post a Comment