-
Notifications
You must be signed in to change notification settings - Fork 37
Net::Twitter and the death of Basic Authentication
On August 31, 2010, Twitter discontinued Basic Authentication.
That means, if you're using a username
and password
with
Net::Twitter or Net::Twitter::Lite, you will get 401 Unauthorized
errors attempting to access the Twitter API.
Twitter now requires OAuth authentication for access to the Twitter API. The good news is: Net::Twitter and Net::Twitter::Lite fully support OAuth. So the transition should be as pain free as possible.
For simple, automated scripts, all you need to do is register a twitter
application, copy the consumer key and secret and the access_token
and secret, and use them instead of username
and password
.
use Net::Twitter;
my $nt = Net::Twitter->new(
traits => [qw/API::REST OAuth/],
consumer_key => $YOUR_CONSUMER_KEY,
consumer_secret => $YOUR_CONSUMER_SECRET,
access_token => $YOUR_ACCESS_TOKEN,
access_token_secret => $YOUR_ACCESS_TOKEN_SECRET,
);
$nt->update("Bob's your uncle!");
With Net::Twitter::Lite, it's even slightly simpler:
use Net::Twitter::Lite;
my $nt = Net::Twitter::Lite->new(
consumer_key => $YOUR_CONSUMER_KEY,
consumer_secret => $YOUR_CONSUMER_SECRET,
access_token => $YOUR_ACCESS_TOKEN,
access_token_secret => $YOUR_ACCESS_TOKEN_SECRET,
);
$nt->update("Bob's your uncle!");
Of course, with Net::Twitter::Lite, you don't get all the bells and whistles, like the RetryOnError trait that vanquishes many Fail Whales. Or the InflateObjects trait that turns created_at dates into DateTime objects, and other goodies.
If you have a web application or interactive program that needs to obtain access tokens for multiple users, that's easy, too. See the Net::Twitter examples directory or the Net::Twitter::Lite examples directoryin their respective distributions.
For desktop applications where the normal web OAuth flow, or PIN-code
out-of-band flow is not practical, Twitter offers xAuth for
exchanging a username
and password
for access tokens. Access to
xAuth is restricted to approved accounts. You can request access by
emailing api@twitter.com. Once approved, use the xauth
method in
Net::Twitter or Net::Twitter::Lite to obtain access tokens:
my ( $token, $secret, $user_id, $screen_name )
= $nt->xauth($username, $password);
To stay informed, you should:
- Follow @perl_api on Twitter.
- Follow @twitterapi on Twitter.
- Subscribe to the Twitter API Announcements List.
Happy coding.