The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::Twitter::OAuth - Net::Twitter subclass that uses OAuth instead of Basic Auth

SYNOPSIS

  use Net::Twitter::OAuth;

  my $client = Net::Twitter::OAuth->new(
      consumer_key    => "YOUR-CONSUMER-KEY",
      consumer_secret => "YOUR-CONSUMER-SECRET",
  );

  # Do some Authentication work. See EXAMPLES

  my $tweets = $client->friends_timeline;
  my $res    = $client->update({ status => "I CAN HAZ OAUTH!" });

DESCRIPTION

Net::Twitter::OAuth is a Net::Twitter subclass that uses OAuth authentication instead of the default Basic Authentication.

Note that this client only works with APIs that are compatible to OAuth authentication.

EXAMPLES

Here's how to authorize users as a desktop app mode:

  use Net::Twitter::OAuth;

  my $client = Net::Twitter::OAuth->new(
      consumer_key    => "YOUR-CONSUMER-KEY",
      consumer_secret => "YOUR-CONSUMER-SECRET",
  );

  # You'll save these token in a config file or app registry
  my($access_token, $access_token_secret) = restore_tokens();
  if ($access_token && $access_token_secret) {
      $client->oauth->access_token($access_token);
      $client->oauth->access_token_secret($access_token_secret);
  }

  unless ($client->oauth->authorized) {
      # The client is not yet authorized: Do it now
      print "Authorize this app at ", $client->oauth->get_authorization_url, " and hit RET\n";

      <STDIN>; # wait for input

      my($access_token, $access_token_secret) = $client->oauth->request_access_token;
      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 $client = Net::Twitter::OAuth->new(%param);
      my $url = $client->oauth->get_authorization_url;

      $c->response->cookies->{oauth} = {
          value => {
              token => $client->oauth->request_token,
              token_secret => $client->oauth->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 $client = Net::Twitter::OAuth->new(%param);
      $client->oauth->request_token($cookie{token});
      $client->oauth->request_token_secret($cookie{token_secret});

      my($access_token, $access_token_secret)
          = $client->oauth->request_access_token;

      # Save $access_token and $access_token_secret in the database
      save_oauth_tokens($c->user);
  }

Later on, you can retrieve and reset those access token and secret before calling any Twitter API methods.

  sub make_tweet : Local {
      my($self, $c) = @_;

      my($access_token, $access_token_secret) = restore_oauth_tokens($c->user);

      my $client = Net::Twitter::OAuth->new(%param);
      $client->oauth->access_token($access_token);
      $client->oauth->access_token_secret($access_token_secret);

      # Now you can call any Net::Twitter API methods on $client
      my $status = $c->req->param('status');
      my $res = $client->update({ status => $status });
  }

METHODS

new
  $client = Net::Twitter::OAuth->new(
      consumer_key => $consumer_key,
      consumer_secret => $consumer_secret,
  );

Creates a new Net::Twitter::OAuth object. Takes the parameters consumer_key and consumer_secret that can be acquired at Twitter Developer screen http://twitter.com/oauth_clients.

oauth
  $client->oauth;

Returns Net::OAuth::Simple object to deal with getting and setting OAuth tokens. See Net::OAuth::Simple for details.

AUTHOR

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Net::Twitter, Net::OAuth::Simple