Net::OAuth::Simple - a simple wrapper round the OAuth protocol
First create a sub class of Net::OAuth::Simple that will do you requests for you.
Net::OAuth::Simple
package Net::AppThatUsesOAuth; use strict; use base qw(Net::OAuth::Simple); sub new { my $class = shift; my %tokens = @_; return $class->SUPER::new( tokens => \%tokens, urls => { authorization_url => ..., request_token_url => ..., access_token_url => ..., }); } sub view_restricted_resource { my $self = shift; return $self->make_restriced_request($url, 'GET'); } sub update_restricted_resource { my $self = shift return $self->make_restriced_request($url, 'POST', %extra_params); } 1;
Then in your main app you need to do
my %tokens = get_tokens(); my $app = Net::AppThatUsesOAuth->new(%tokens); # Check to see we have a consumer key and secret unless ($app->consumer_key && $app->consumer_key) { die "You must go get a consumer key and secret from App\n"; } # If the app is authorized (i.e has an access token and secret) # Then look at a restricted resourse if ($app->authorized) { my $response = $app->view_restricted_resource; print $response->content."\n"; exit; } # Otherwise the user needs to go get an access token and secret print "Go to ".$app->get_authorization_url."\n"; print "Then hit return after\n"; <STDIN>; my ($access_token, $access_token_secret) = $app->request_access_token; # Now save those values
Note the flow will be somewhat different for web apps since the request token and secret will need to be saved whilst the user visits the authorization url.
For examples go look at the Net::FireEagle module and the fireeagle command line script that ships with it. Also in the same distribution in the examples/ directory is a sample web app.
Net::FireEagle
fireeagle
examples/
Create a new OAuth enabled app - takes a hash of params.
One of the keys of the hash must be tokens, the value of which must be a hash ref with the keys:
tokens
Then, when you have your per-use access token and secret you can supply
Another key of the hash must be urls, the value of which must be a hash ref with the keys
urls
Whether the client has the necessary credentials to be authorized.
Note that the credentials may be wrong and so the request may still fail.
The signature method to use.
Defaults to HMAC-SHA1
Get all the tokens.
Returns the current consumer key.
Can optionally set the consumer key.
Returns the current consumer secret.
Can optionally set the consumer secret.
Returns the current access token.
Can optionally set a new token.
Returns the current access token secret.
Can optionally set a new secret.
Returns the current request token.
Returns the current request token secret.
Get the url the user needs to visit to authorize.
Note: this is the base url - not the full url with the necessary OAuth params.
Get the url to obtain a request token.
Get the url to obtain an access token.
Request the access token and access token secret for this user.
The user must have authorized this app at the url given by get_authorization_url first.
get_authorization_url
Returns the access token and access token secret but also sets them internally so that after calling this method you can immediately call location or update_location.
location
update_location
Get the URL to authorize a user.
Make a request to url using the given HTTP method.
url
Any extra parameters can be passed in as a hash.
A convenience method for loading tokens from a config file.
Returns a hash with the token names suitable for passing to new().
new()
Returns an empty hash if the file doesn't exist.
A convenience method to save a hash of tokens out to the given file.
If Math::Random::MT is installed then any nonces generated will use a Mersenne Twiser instead of Perl's built in randomness function.
Math::Random::MT
Non known
The latest code for this module can be found at
https://svn.unixbeard.net/simon/Net-OAuth-Simple
Simon Wistow, <simon@thegestalt.org >
<simon@thegestalt.org
Please report any bugs or feature requests to bug-net-oauth-simple at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-OAuth-Simple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-net-oauth-simple at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Net::OAuth::Simple
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-OAuth-Simple
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Net-OAuth-Simple
CPAN Ratings
http://cpanratings.perl.org/d/Net-OAuth-Simple
Search CPAN
http://search.cpan.org/dist/Net-OAuth-Simple/
Copyright 2009 Simon Wistow, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Net::OAuth::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::OAuth::Simple
CPAN shell
perl -MCPAN -e shell install Net::OAuth::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.