perldoc > Net::Twitter::Role::OAuth

📖 NAME

Net::Twitter::Role::OAuthNet::Twitter role that provides OAuth instead of Basic Authentication

🚀 Quick Reference

Use Case Command Description
Desktop app authorization $nt->get_authorization_url → PIN → $nt->request_access_token(verifier => $pin) Get URL, user enters PIN, exchange for access tokens
Web app authorization $nt->get_authorization_url(callback => $url) → save request token → $nt->request_access_token(verifier => $verifier) Redirect user to Twitter, handle callback, upgrade to access token
Use stored access tokens $nt->access_token($token); $nt->access_token_secret($secret); Set saved tokens, then call API methods directly

📌 VERSION

version 4.01043

💻 SYNOPSIS

  use Net::Twitter;

  my $nt = Net::Twitter->new(
      traits          => ['API::RESTv1_1', 'OAuth'],
      consumer_key    => "YOUR-CONSUMER-KEY",
      consumer_secret => "YOUR-CONSUMER-SECRET",
  );

  # Do some Authentication work. See EXAMPLES

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

📝 DESCRIPTION

Net::Twitter::Role::OAuth is a Net::Twitter role that provides OAuth authentication instead of the default Basic Authentication.

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

⚠️ IMPORTANT

Beginning with version 3.02, it is necessary for web applications to pass the "callback" parameter to get_authorization_url. In the absence of a callback parameter, when the user authorizes the application a PIN number is displayed rather than redirecting the user back to your site.

📚 EXAMPLES

See the "examples" directory in this distribution for working examples of both desktop and web applications.

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

  use Net::Twitter;

  my $nt = Net::Twitter->new(
      traits          => ['API::RESTv1_1', 'OAuth'],
      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->new(traits => [qw/API::RESTv1_1 OAuth/], %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 $verifier = $c->req->params->{oauth_verifier};

      my $nt = Net::Twitter->new(traits => [qw/API::RESTv1_1 OAuth/], %param);
      $nt->request_token($cookie{token});
      $nt->request_token_secret($cookie{token_secret});

      my($access_token, $access_token_secret, $user_id, $screen_name)
          = $nt->request_access_token(verifier => $verifier);

      # Save $access_token and $access_token_secret in the database associated with $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) = ...;

      my $nt = Net::Twitter->new(traits => [qw/API::RESTv1_1 OAuth/], %param);
      $nt->access_token($access_token);
      $nt->access_token_secret($access_token_secret);

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

🔧 METHODS

🗑️ DEPRECATED METHODS

🙏 ACKNOWLEDGEMENTS

This module was originally authored by Tatsuhiko Miyagawa as "Net::Twitter::OAuth", a subclass of the "Net::Twitter" 2.x. It was refactored into a Moose Role for use in "Net::Twitter" 3.0 and above by Marc Mims. Many thanks to Tatsuhiko for the original work on both code and documentation.

👤 AUTHORS

📜 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::Twitter::OAuth::Simple, Net::OAuth::Simple

Net::Twitter::Role::OAuth
📖 NAME 🚀 Quick Reference 📌 VERSION 💻 SYNOPSIS 📝 DESCRIPTION ⚠️ IMPORTANT 📚 EXAMPLES 🔧 METHODS 🗑️ DEPRECATED METHODS 🙏 ACKNOWLEDGEMENTS 👤 AUTHORS 📜 LICENSE 🔗 SEE ALSO

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-22 01:35 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^