AnyEvent::Twitter - A thin wrapper for Twitter API using OAuth
use utf8; use Data::Dumper; use AnyEvent; use AnyEvent::Twitter; my $ua = AnyEvent::Twitter->new( consumer_key => 'consumer_key', consumer_secret => 'consumer_secret', access_token => 'access_token', access_token_secret => 'access_token_secret', ); # or my $ua = AnyEvent::Twitter->new( consumer_key => 'consumer_key', consumer_secret => 'consumer_secret', token => 'access_token', token_secret => 'access_token_secret', ); # or, if you use eg/gen_token.pl, you can write simply as: use JSON; use Perl6::Slurp; my $json_text = slurp 'config.json'; my $config = decode_json($json_text); my $ua = AnyEvent::Twitter->new(%$config); my $cv = AE::cv; # GET request $cv->begin; $ua->get('account/verify_credentials', sub { my ($hdr, $res, $reason) = @_; say $res->{screen_name}; $cv->end; }); # GET request with parameters $cv->begin; $ua->get('account/verify_credentials', {include_entities => 1}, sub { my ($hdr, $res, $reason) = @_; say $res->{screen_name}; $cv->end; }); # POST request with parameters $cv->begin; $ua->post('statuses/update', {status => 'いろはにほへと ちりぬるを'}, sub { my ($hdr, $res, $reason) = @_; say $res->{user}{screen_name}; $cv->end; }); # verbose and old style $cv->begin; $ua->request( method => 'GET', api => 'account/verify_credentials', sub { my ($hdr, $res, $reason) = @_; unless ($res) { print $reason, "\n"; } else { print "ratelimit-remaining : ", $hdr->{'x-ratelimit-remaining'}, "\n", "x-ratelimit-reset : ", $hdr->{'x-ratelimit-reset'}, "\n", "screen_name : ", $res->{screen_name}, "\n"; } $cv->end; } ); $cv->begin; $ua->request( method => 'POST', api => 'statuses/update', params => { status => 'hello world!' }, sub { print Dumper \@_; $cv->end; } ); $cv->begin; $ua->request( method => 'POST', url => 'http://api.twitter.com/1/statuses/update.json', params => { status => 'いろはにほへと ちりぬるを' }, sub { print Dumper \@_; $cv->end; } ); $cv->recv;
AnyEvent::Twitter is a very thin wrapper for Twitter API using OAuth.
All arguments are required. If you don't know how to obtain these parameters, take a look at eg/gen_token.pl and run it.
These parameters are required.
The api parameter is a shortcut option.
api
If you want to specify the API url, the url parameter is good for you. The format should be 'json'.
url
The api parameter will be internally processed as:
$url = 'http://api.twitter.com/1/' . $opt{api} . '.json';
You can check the api option at Twitter API Wiki
Investigate the HTTP method and required parameters of Twitter API that you want to use. Then specify it. GET/POST methods are allowed. You can omit params if Twitter API doesn't requires option.
params
This module is AnyEvent::HTTP style, so you have to pass the coderef callback.
Passed callback will be called with $hdr, $response and $reason. If something is wrong with the response from Twitter API, $response will be undef. So you can check the value like below.
$hdr
$response
$reason
undef
sub { my ($hdr, $res, $reason) = @_; unless ($res) { print $reason, "\n"; } else { print $res->{screen_name}, "\n"; } }
He gave me plenty of test code.
He cleaned my code up.
punytan <punytan@gmail.com>
AnyEvent::HTTP, Net::OAuth
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install AnyEvent::Twitter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm AnyEvent::Twitter
CPAN shell
perl -MCPAN -e shell install AnyEvent::Twitter
For more information on module installation, please visit the detailed CPAN module installation guide.