NAME
AnyEvent::Twitter - A thin wrapper for Twitter API using OAuth
SYNOPSIS
use utf8;
use Data::Dumper;
use AnyEvent;
use AnyEvent::Twitter;
my $ua = AnyEvent::Twitter->new(
consumer_key => 'consumer_key',
consumer_secret => 'consumer_secret',
token => 'access_token',
token_secret => 'access_token_secret',
);
# or
my $ua = AnyEvent::Twitter->new(
consumer_key => 'consumer_key',
consumer_secret => 'consumer_secret',
access_token => 'access_token',
access_token_secret => 'access_token_secret',
);
# or, if you use eg/gen_token.pl, you can write simply as:
my $json_text = slurp 'config.json';
my $config = JSON::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 ($header, $response, $reason) = @_;
say $response->{screen_name};
$cv->end;
});
# GET request with parameters
$cv->begin;
$ua->get('account/verify_credentials', {
include_entities => 1
}, sub {
my ($header, $response, $reason) = @_;
say $response->{screen_name};
$cv->end;
});
# POST request with parameters
$cv->begin;
$ua->post('statuses/update', {
status => 'いろはにほへと ちりぬるを'
}, sub {
my ($header, $response, $reason) = @_;
say $response->{user}{screen_name};
$cv->end;
});
# verbose and old style
$cv->begin;
$ua->request(
method => 'GET',
api => 'account/verify_credentials',
sub {
my ($hdr, $res, $reason) = @_;
if ($res) {
print "ratelimit-remaining : ", $hdr->{'x-ratelimit-remaining'}, "\n",
"x-ratelimit-reset : ", $hdr->{'x-ratelimit-reset'}, "\n",
"screen_name : ", $res->{screen_name}, "\n";
} else {
say $reason;
}
$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;
DESCRIPTION
AnyEvent::Twitter is a very thin wrapper for Twitter API using OAuth.
API VERSION
As of version 0.63, AnyEvent::Twitter supports Twitter REST API v1.1.
NOTE: API version 1.0 is already deprecated.
METHODS
new
All arguments are required except api_version
. If you don't know how to obtain these parameters, take a look at eg/gen_token.pl and run it.
consumer_key
consumer_secret
access_token
(ortoken
)access_token_secret
(ortoken_secret
)api_version
(optional; default: 1.1)-
If you have a problem with API changes, specify
api_version
parameter. Possible values are:1.1
or1.0
get
$ua->get($api, sub {})
$ua->get($api, \%params, sub {})
$ua->get($url, sub {})
$ua->get($url, \%params, sub {})
post
$ua->post($api, \%params, sub {})
$ua->post($url, \%params, sub {})
$ua->post($api, \@params, sub {})
$ua->post($url, \@params, sub {})
UPLOADING MEDIA FILE
You can use statuses/update_with_media
API to upload photos by specifying parameters as arrayref like below example.
Uploading photos will be tranferred with Content-Type multipart/form-data
(not application/x-www-form-urlencoded
)
use utf8;
$ua->post(
'statuses/update_with_media',
[
status => '桜',
'media[]' => [ undef, $filename, Content => $loaded_image_binary ],
],
sub {
my ($hdr, $res, $reason) = @_;
say $res->{user}{screen_name};
}
);
request
These parameters are required.
api
orurl
-
The
api
parameter is a shortcut option.If you want to specify the API
url
, theurl
parameter is good for you. The format should be 'json'.The
api
parameter will be internally processed as:sprintf 'https://api.twitter.com/1.1/%s.json', $api; # version 1.1 sprintf 'http://api.twitter.com/1/%s.json', $api; # version 1.0
You can find available
api
s at API Documentation method
andparams
-
Investigate the HTTP method and required parameters of Twitter API that you want to use. Then specify it. GET and POST methods are allowed. You can omit
params
if Twitter API doesn't require it. - callback
-
This module is AnyEvent::HTTP style, so you have to pass the callback (coderef).
Passed callback will be called with
$header
,$response
,$reason
and$error_response
. If something is wrong with the response from Twitter API,$response
will beundef
. On non-2xx HTTP status code, you can get the decoded response via$error_response
. So you can check the value like below.my $callback = sub { my ($header, $response, $reason, $error_response) = @_; if ($response) { say $response->{screen_name}; } else { say $reason; for my $error (@{$error_response->{errors}}) { say "$error->{code}: $error->{message}"; } } };
parse_timestamp
parse_timestamp
parses created_at
timestamp like "Thu Mar 01 17:38:56 +0000 2012". It returns Time::Piece object. Its timezone is localtime.
TESTS
Most of all tests are written as author tests since this module depends on remote API server. So if you want read code that works well, take a look at xt/
directory.
EXPERIMENTAL METHODS
Methods listed below are experimental feature. So interfaces or returned values may vary in the future.
AnyEvent::Twitter->get_request_token
AnyEvent::Twitter->get_request_token(
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
callback_url => 'http://example.com/callback',
# auth => 'authenticate',
cb => sub {
my ($location, $response, $body, $header) = @_;
# $location is the endpoint where users are asked the permission
# $response is a hashref of parsed body
# $body is raw response itself
# $header is response headers
},
);
AnyEvent::Twitter->get_access_token
AnyEvent::Twitter->get_access_token(
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
oauth_token => $oauth_token,
oauth_token_secret => $oauth_token_secret,
oauth_verifier => $oauth_verifier,
cb => sub {
my ($token, $body, $header) = @_;
# $token is the parsed body
# $body is raw response
# $header is response headers
},
);
CONTRIBUTORS
AUTHOR
punytan <punytan@gmail.com>
SEE ALSO
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.