Net-Twitter-Lite
view release on metacpan or search on metacpan
# in .netrc
machine api.twitter.com
login YOUR_TWITTER_USER_NAME
password YOUR_TWITTER_PASSWORD
machine semifor.twitter.com
login semifor
password SUPERSECRET
# in your perl program
$nt = Net::Twitter::Lite->new(netrc => 1);
$nt = Net::Twitter::Lite->new(netrc => 'semifor.twitter.com');
netrc_machine
(Optional) Sets the machine entry to look up in .netrc when <netrc
= 1>> is used. Defaults to api.twitter.com.
legacy_lists_api
If set to 1, this option enables backwards compatibility by using
the now deprecated endpoints and semantics for lists API methods.
If set to 0, the new endpoints and semantics will be used. Only the
new lists API methods are documented here.
If you do not provide this option to new a warning is issued.
Support for this option and the legacy lists API methods will be
removed in a future version.
wrap_result
(Optional) If set to 1, this option will return an
Net::Twitter::Lite::WrapResult object, which provides both the
Twitter API result and the HTTP::Response object for the API call.
See Net::Twitter::Lite::WrapResult for details.
BASIC AUTHENTICATION METHODS
credentials($username, $password)
Set the credentials for Basic Authentication. This is helpful for
managing multiple accounts.
OAUTH METHODS
authorized
Whether the client has the necessary credentials to be authorized.
Note that the credentials may be wrong and so the request may fail.
request_access_token
Returns list including the access token, access token secret,
user_id, and screen_name for this user. Takes a HASH of arguments.
The verifier argument is required. See "OAUTH EXAMPLES".
The user must have authorized this app at the url given by
get_authorization_url first.
For desktop applications, the Twitter authorization page will present
the user with a PIN number. Prompt the user for the PIN number, and
pass it as the verifier argument to request_access_token.
Returns the access token and access token secret but also sets them
internally so that after calling this method, you can immediately
call API methods requiring authentication.
get_authorization_url(callback => $callback_url)
Get the URL used to authorize the user. Returns a URI object. For web
applications, pass your applications callback URL as the callback
parameter. No arguments are required for desktop applications
(callback defaults to oob, out-of-band).
get_authentication_url(callback => $callback_url)
Get the URL used to authenticate the user with "Sign in with Twitter"
authentication flow. Returns a URI object. For web applications, pass
your applications callback URL as the callback parameter. No
arguments are required for desktop applications (callback defaults to
oob, out-of-band).
xauth($username, $password)
Exchanges a username and password for OAuth tokens. Your application
must be approved for XAuth access by Twitter for this method to work.
Twitter does not grant XAuth access for web applications except for a
brief period of time to allow them to switch form Basic
authentication to OAuth authentication.
access_token
Get or set the access token.
access_token_secret
Get or set the access token secret.
request_token
Get or set the request token.
request_token_secret
Get or set the request token secret.
access_token_url
Get or set the access_token URL.
authentication_url
Get or set the authentication URL.
authorization_url
Get or set the authorization URL.
request_token_url
Get or set the request_token URL.
xauth_url
Get or set the XAuth access token request URL.
API METHODS AND ARGUMENTS
Most Twitter API methods take parameters. All Net::Twitter::Lite API
methods will accept a HASH ref of named parameters as specified in the
Twitter API documentation. For convenience, many Net::Twitter::Lite
methods accept simple positional arguments as documented, below. The
positional parameter passing style is optional; you can always use the
named parameters in a hash ref if you prefer.
For example, the REST API method update has one required parameter,
status. You can call update with a HASH ref argument:
$nt->update({ status => 'Hello world!' });
When Net::Twitter::Lite encounters a Twitter API error or a network
error, it throws a Net::Twitter::Lite::Error object. You can catch and
process these exceptions by using eval blocks and testing $@:
eval {
my $statuses = $nt->friends_timeline(); # this might die!
for my $status ( @$statuses ) {
#...
}
};
if ( $@ ) {
# friends_timeline encountered an error
if ( blessed $@ && $@->isa('Net::Twitter::Lite::Error' ) {
#... use the thrown error obj
warn $@->error;
}
else {
# something bad happened!
die $@;
}
}
Net::Twitter::Lite::Error stringifies to something reasonable, so if
you don't need detailed error information, you can simply treat $@ as a
string:
eval { $nt->update($status) };
if ( $@ ) {
warn "update failed because: $@\n";
}
AUTHENTICATION
Net::Twitter::Lite currently supports both Basic Authentication and
OAuth. The choice of authentication strategies is determined by the
options passed to new or the use of the credentials method. An error
will be thrown if options for both strategies are provided.
BASIC AUTHENTICATION
To use Basic Authentication, pass the username and password options to
new, or call credentials to set them. When Basic Authentication is
used, the Authorization header is set on each authenticated API call.
OAUTH AUTHENTICATION
To use OAuth authentication, pass the consumer_key and consumer_secret
options to new.
Net::OAuth::Simple must be installed in order to use OAuth and an error
will be thrown if OAuth is attempted without it. Net::Twitter::Lite
does not require Net::OAuth::Simple, making OAuth an optional feature.
OAUTH EXAMPLES
See the examples directory included in this distribution for full
working examples using OAuth.
Here's how to authorize users as a desktop app mode:
use Net::Twitter::Lite;
my $nt = Net::Twitter::Lite->new(
consumer_key => "YOUR-CONSUMER-KEY",
consumer_secret => "YOUR-CONSUMER-SECRET",
);
# You'll save the token and secret in cookie, config file or session database
my($access_token, $access_token_secret) = restore_tokens();
if ($access_token && $access_token_secret) {
$nt->access_token($access_token);
$nt->access_token_secret($access_token_secret);
}
unless ( $nt->authorized ) {
# The client is not yet authorized: Do it now
print "Authorize this app at ", $nt->get_authorization_url, " and enter the PIN#\n";
my $pin = <STDIN>; # wait for input
chomp $pin;
my($access_token, $access_token_secret, $user_id, $screen_name) =
$nt->request_access_token(verifier => $pin);
save_tokens($access_token, $access_token_secret); # if necessary
}
# Everything's ready
In a web application mode, you need to save the oauth_token and
oauth_token_secret somewhere when you redirect the user to the OAuth
authorization URL.
sub twitter_authorize : Local {
my($self, $c) = @_;
my $nt = Net::Twitter::Lite->new(%param);
my $url = $nt->get_authorization_url(callback => $callbackurl);
$c->response->cookies->{oauth} = {
value => {
token => $nt->request_token,
token_secret => $nt->request_token_secret,
},
};
$c->response->redirect($url);
}
And when the user returns back, you'll reset those request token and
secret to upgrade the request token to access token.
sub twitter_auth_callback : Local {
my($self, $c) = @_;
my %cookie = $c->request->cookies->{oauth}->value;
my $nt = Net::Twitter::Lite->new(%param);
$nt->request_token($cookie{token});
$nt->request_token_secret($cookie{token_secret});
( run in 2.352 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )