Net-OAuth-Simple
view release on metacpan or search on metacpan
lib/Net/OAuth/Simple.pm view on Meta::CPAN
my ($key, $val) = split /\s*=\s*/, $_, 2;
$tokens{$key} = $val;
});
return %tokens;
}
=head2 save_tokens <file> [token[s]]
A convenience method to save a hash of tokens out to the given file.
=cut
sub save_tokens {
my $class = shift;
my $file = shift;
my %tokens = @_;
my $max = 0;
foreach my $key (keys %tokens) {
$max = length($key) if length($key)>$max;
}
open(my $fh, ">$file") || die "Couldn't open $file for writing: $!\n";
foreach my $key (sort keys %tokens) {
my $pad = " "x($max-length($key));
print $fh "$key ${pad}= ".$tokens{$key}."\n";
}
close($fh);
}
sub _read_file {
my $self = shift;
my $file = shift;
my $sub = shift;
open(my $fh, $file) || die "Couldn't open $file: $!\n";
while (<$fh>) {
$sub->($_) if $sub;
}
close($fh);
}
=head1 ERROR HANDLING
Originally this module would die upon encountering an error (inheriting behaviour
from the original Yahoo! code).
This is still the default behaviour however if you now pass
return_undef_on_error => 1
into the constructor then all methods will return undef on error instead.
The error message is accessible via the C<last_error()> method.
=head1 GOOGLE'S SCOPE PARAMETER
Google's OAuth API requires the non-standard C<scope> parameter to be set
in C<request_token_url>, and you also explicitly need to pass an C<oauth_callback>
to C<get_authorization_url()> method, so that you can direct the user to your site
if you're authenticating users in Web Application mode. Otherwise Google will let
user grant acesss as a desktop app mode and doesn't redirect users back.
Here's an example class that uses Google's Portable Contacts API via OAuth:
package Net::AppUsingGoogleOAuth;
use strict;
use base qw(Net::OAuth::Simple);
sub new {
my $class = shift;
my %tokens = @_;
return $class->SUPER::new(
tokens => \%tokens,
urls => {
request_token_url => "https://www.google.com/accounts/OAuthGetRequestToken?scope=http://www-opensocial.googleusercontent.com/api/people",
authorization_url => "https://www.google.com/accounts/OAuthAuthorizeToken",
access_token_url => "https://www.google.com/accounts/OAuthGetAccessToken",
},
);
}
package main;
my $oauth = Net::AppUsingGoogleOAuth->new(%tokens);
# Web application
$app->redirect( $oauth->get_authorization_url( callback => "http://you.example.com/oauth/callback") );
# Desktop application
print "Open the URL and come back once you're authenticated!\n",
$oauth->get_authorization_url;
See L<http://code.google.com/apis/accounts/docs/OAuth.html> and other
services API documentation for the possible list of I<scope> parameter value.
=head1 RANDOMNESS
If C<Math::Random::MT> is installed then any nonces generated will use a
Mersenne Twiser instead of Perl's built in randomness function.
=head1 EXAMPLES
There are example Twitter and Twitter xAuth 'desktop' apps and a FireEagle OAuth 1.0a web app
in the examples directory of the distribution.
=head1 BUGS
Non known
=head1 DEVELOPERS
The latest code for this module can be found at
https://svn.unixbeard.net/simon/Net-OAuth-Simple
=head1 AUTHOR
Simon Wistow, C<<simon@thegestalt.org>>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-oauth-simple at rt.cpan.org>, or through
the web interface at L<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.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::OAuth::Simple
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-OAuth-Simple>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-OAuth-Simple>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Net-OAuth-Simple>
=item * Search CPAN
L<http://search.cpan.org/dist/Net-OAuth-Simple/>
=back
=head1 COPYRIGHT & LICENSE
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.
=cut
( run in 1.496 second using v1.01-cache-2.11-cpan-d06a3f9ecfd )