Google-OAuth
view release on metacpan or search on metacpan
samples/Facebook/OAuth.pm view on Meta::CPAN
}
## If token expires, it's too late. Facebook expiration is unrecoverable.
sub expired {
my $self = shift ;
return $self->{requested} +$self->{expires} < time ;
}
## Facebook has no header requirements
sub headers {
return () ;
}
sub request {
my $self = shift ;
my $method = @_ > 1? shift @_: 'GET' ;
my $url = shift ;
my @url = split /\?/, $url ;
$url[1] ||= '' ;
my $where = join '&', sprintf( '%s?access_token=%s', $url[0],
$self->{access_token} ), $url[1] ;
return Google::OAuth::Request::request( $self, $method, $where ) ;
}
1 ;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Facebook::OAuth - Extends Google::OAuth for Facebook
=head1 SYNOPSIS
use Facebook::OAuth;
use base qw( Google::OAuth ) ;
## Get Grant Code
$link = Facebook::OAuth->new->scope( ... )->token_request ;
$link = Facebook::OAuth->token_request ; ## use defaults
## Generate Token
Facebook::OAuth->grant_code( $code ) ;
## Access Facebook
$fbo = Facebook::OAuth->token( $email )->content( GET => $url ) ;
$fbo = Facebook::OAuth->token( $email )->content( $url ) ;
=head1 DESCRIPTION
Facebooks's OAuth implementation is much simpler than Google's, so this
interface may be over-generalized. The advantage lies in code reuse, and
a consistent interface that ultimately provides an effective general
purpose OAuth client.
The Google::OAuth setup includes a Facebook configuration to establish
Facebook credentials. The configuration assumes a single data source
for all OAuth tokens that can support a variety of web services.
Additionally, the L<SYNOPSIS> demonstrates an API showing the 3 phases
of data access:
=head2 Acquire a Grant Code
In order to generate a I<Grant Code>, users log into Facebook and the grant
code is transmitted to a I<redirect_uri> defined in the credentials. All the
credentials are passed as query parameters in a single URI link.
Facebook uses quite a few permission settings, any number of which can be
passed as arguments to the C<scope()> method. This interface always requests
the I<email> permisssion.
## Get Grant Code
$link = Facebook::OAuth->token_request ;
=head2 Acquire a Token
Facebook returns a temporary I<grant code> that needs to be resubmitted to
obtain a token. The grant code is transmitted to a webserver via the
I<redirect_uri> so the token is usually acquired by a process owned by the
webserver.
## Generate Token
Facebook::OAuth->grant_code( $code ) ;
The C<grant_code> method saves the results in the data source. Since it
normally returns a volatile object, the following invocation is recommended
to examine the results:
%status = %{ Facebook::OAuth->grant_code( $code, $email ) } ;
=head2 Refresh Token - Access Facebook
A Facebook token can be renewed indefinitely, but the expiration policy is
I<Use it or lose it>. Google requires that a token be renewed before using.
Use the same approach to ensure that a Facebook token is continuously renewed
as follows:
## $fobj - Facebook data object
## $url - Use Facebook API
$fobj = Facebook::OAuth->token( $email )->content( GET => $url ) ;
## If necessary, select an email key from a list:
@email = Facebook::OAuth->token_list ;
## If the token is to be reused:
$token = Facebook::OAuth->token( $email ) ;
$fobj = $token->content( GET => $url ) ;
## GET is the default method, so the following works:
$fobj = $token->content( $url ) ;
=head1 METHODS
The following methods are overridden in the Facebook::OAuth subclass:
=over 8
=item setclient()
C<setclient()> replaces the Google configuration parameters with their
Facebook equivalents.
=item classID()
( run in 0.598 second using v1.01-cache-2.11-cpan-39bf76dae61 )