Net-Facebook-Oauth2-0.09/ 0000744 0000000 0000000 00000000000 12504712664 013542 5 ustar root root Net-Facebook-Oauth2-0.09/examples/ 0000744 0000000 0000000 00000000000 12504712661 015355 5 ustar root root Net-Facebook-Oauth2-0.09/examples/CGI/ 0000744 0000000 0000000 00000000000 12504712661 015757 5 ustar root root Net-Facebook-Oauth2-0.09/examples/CGI/facebook.pl 0000644 0000000 0000000 00000007451 12504331154 020070 0 ustar root root #!/usr/bin/perl
use strict;
use warnings;
use CGI;
use Net::Facebook::Oauth2;
use Data::Dumper;
=head1 DESCRIPTION
This is a general example of using use Net::Facebook::Oauth2;
For a better understanding I recommend you to look at the Catalyst example
Don't worry if you are not familiar with catalyst, I tried to make it as simple
as I can with no complication, plus Catalyst is awsome :)
=cut
sub facebook {
my $cgi = CGI->new;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id', ##get this from your facebook developers platform
application_secret => 'your_application_secret', ##get this from your facebook developers platform
callback => 'http://your-domain.com/callback', ##Callback URL, facebook will redirect users after authintication
);
my $url = $fb->get_authorization_url(
scope => ['offline_access','publish_stream', 'user_friends'], ###pass scope/Extended Permissions params as an array telling facebook how you want to use this access
display => 'page' ## how to display authorization page, other options popup "to display as popup window" and wab "for mobile apps"
);
###scope/Extended Permissions description
##offline_access : Allow your application to edit profile while user is not online
##publish_stream : read write access
##user_friends: list of user's friends
##you can find more about facebook scopes/Extended Permissions at
##http://developers.facebook.com/docs/authentication/permissions
##now redirect to the authorization page
print $cgi->redirect($url);
}
sub callback {
##this sub represent the callback block, where facebook will send users back upon authorization
my $cgi = CGI->new;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://your-domain.com/callback'
);
####We recieve "verifier" code parameter, now get access token
###you need to pass the verifier code to get access_token
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
##that's it, now you have access_token of this user
###save this token in database or session to use later in your application
save_access_token($access_token);
print $cgi->header();
print "Welcome";
}
###get information about this user from Facebook, use get method
sub get {
my $cgi = CGI->new;
###now any time you want to get information about user, just retrieve saved access_token of that use
###pass it to
###and call facebook Graph API URL
###for example let's get friends list of that user
my $access_token = get_access_token('userid'); ###this is a demo, no real get_access_token sub here :P
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token ##Load previous saved access token from session or database
);
my $friends = $fb->get(
'https://graph.facebook.com/v2.2/me/friends' ##Facebook friends list API URL
);
print $cgi->header();
print $friends->as_json;
}
###to post something to facebook use post method
sub post {
my $cgi = CGI->new;
###Lets post a message to the feed of the authorized user
my $access_token = get_access_token('userid');
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token
);
my $res = $fb->post(
'https://graph.facebook.com/v2.2/me/feed', ###API URL
{
message => 'This is a post to my feed from Net::Facebook::Oauth2' ##hash of params/variables (param=>value)
}
);
print $cgi->header();
print Dumper($res->as_hash); ##print response as perl hash
}
1;
Net-Facebook-Oauth2-0.09/examples/Catalyst/ 0000744 0000000 0000000 00000000000 12504712661 017141 5 ustar root root Net-Facebook-Oauth2-0.09/examples/Catalyst/Facebook.pm 0000644 0000000 0000000 00000012012 12504331154 021200 0 ustar root root package MyApp::Controller::Facebook;
use strict;
use warnings;
use parent 'Catalyst::Controller';
use Net::Facebook::Oauth2;
=head1 DESCRIPTION
this example shows you how to use Net::Facebook::Oauth2 with Catalyst,
it's not the best way of writing your catalyst controller though :P
The call back URL below is in the same block of
When live test this application don't use localhost, as facebook will
return an error because the callback URL must match the one you set in
facebook developer platfor for your application
=cut
sub index : Private {
my ( $self, $c ) = @_;
my $params = $c->req->parameters;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id', ##get this from your facebook developers platform
application_secret => 'your_application_secret', ##get this from your facebook developers platform
callback => 'http://localhost:3000/facebook', ##Callback URL, facebook will redirect users after authintication
);
#### first check if callback URL doesn't contain a verifier code "code" parameter
if (!$params->{code}){
##there is no verifier code passed so let's create authorization URL and redirect to it
my $url = $fb->get_authorization_url(
scope => ['offline_access','publish_stream', 'user_friends'], ###pass scope/Extended Permissions params as an array telling facebook how you want to use this access
display => 'page' ## how to display authorization page, other options popup "to display as popup window" and wab "for mobile apps"
);
###scope/Extended Permissions description
##offline_access : Allow your application to edit profile while user is not online
##publish_stream : read write access
#user_friends: list of user's friends
##you can find more about facebook scopes/Extended Permissions at
##http://developers.facebook.com/docs/authentication/permissions
$c->res->redirect($url);
}
else {
####second step, we recieved "verifier" code parameters, now get access token
###you need to pass the verifier code to get access_token
my $access_token = $fb->get_access_token( code => $params->{code} );
###save this token in database or session
$c->session->{access_token} = $access_token;
$c->res->body('Welcome from facebook');
}
}
##get/post to facebook on the behalf of the authorized user
##first get this user information, login name, user profile URL
sub get : Local {
my ( $self, $c ) = @_;
my $params = $c->req->parameters;
my $fb = Net::Facebook::Oauth2->new(
access_token => $c->session->{access_token} ##Load previous saved access token from session or database
);
##lets get list of friends for the authorized user
my $info = $fb->get(
'https://graph.facebook.com/v2.2/me' ##Facebook API URL
);
$c->res->body($info->as_json); ##as_json method will print response as json object
}
##example 2 - get friends list for this user
sub get_friends : Local {
my ( $self, $c ) = @_;
my $params = $c->req->parameters;
my $fb = Net::Facebook::Oauth2->new(
access_token => $c->session->{access_token}
);
##lets get list of friends for the authorized user
my $friends = $fb->get(
'https://graph.facebook.com/v2.2/me/friends' ##Facebook 'list friend' Graph API URL
);
$c->res->body($friends->as_json);
}
###example 3 "get" search posts with some keyword
sub search : Local {
my ( $self, $c ) = @_;
my $params = $c->req->parameters;
my $fb = Net::Facebook::Oauth2->new(
access_token => $c->session->{access_token}
);
##lets search all posts with some keyword
##https://graph.facebook.com/search?q=watermelon&type=post
my $topics = $fb->get(
'https://graph.facebook.com/v2.2/search', ##Facebook 'search' Graph API URL
{
q => 'Keyword',
type => 'post'
}
);
$c->res->body($topics->as_json);
}
####post to facebook example
sub post : Local {
my ( $self, $c ) = @_;
my $params = $c->req->parameters;
###Lets post a message to the feed of the authorized user
my $fb = Net::Facebook::Oauth2->new(
access_token => $c->session->{access_token}
);
my $res = $fb->post(
'https://graph.facebook.com/v2.2/me/feed', ###API URL
{
message => 'This is a post to my feed from Net::Facebook::Oauth2' ##hash of params/variables (param=>value)
}
);
$c->res->body($res->as_json);
}
1;
Net-Facebook-Oauth2-0.09/t/ 0000744 0000000 0000000 00000000000 12504712661 014002 5 ustar root root Net-Facebook-Oauth2-0.09/t/accesstoken.t 0000644 0000000 0000000 00000004763 12504331154 016477 0 ustar root root use strict;
use warnings;
use Test::More;
use Net::Facebook::Oauth2;
eval "use Test::Requires qw/Plack::Loader Test::TCP Plack::Request/";
plan skip_all => 'Test::Requires required for testing with Test::TCP' if $@;
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $access_token = $req->param('access_token');
my $message = $req->param('message');
if ($message =~ m/without access token/) {
is $access_token, 'AccessToken', $message;
} elsif ($message =~ m/with access token/) {
is $access_token, 'OtherToken', $message;
} elsif ($message =~ m/with already set query/) {
my $query = $env->{QUERY_STRING};
ok($query !~ /limit=1000\?/);
ok($query =~ /^limit=1000&access_token/);
} elsif ($message =~ m/without already set query/) {
my $query = $env->{QUERY_STRING};
ok($query =~ /^access_token/);
} elsif ($message =~ m/Get request with token and query/) {
my $query = $env->{QUERY_STRING};
ok($query =~ /^limit=1000&access_token=OtherToken&message/);
}
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'OK' ] ];
};
test_tcp(
client => sub {
my $port = shift;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://your-domain.com/callback',
access_token => 'AccessToken',
);
my $url = "http://127.0.0.1:$port";
$fb->post($url, { message => 'Post request without access token' });
$fb->post("$url?access_token=OtherToken", { message => 'Post request with access token' });
$fb->get($url, { message => 'Get request without access token' });
$fb->get("$url?access_token=OtherToken", { message => 'Get request with access token' });
##new bug tests -- adding query to another query ?limit=100?access_token
$fb->get("$url?limit=1000", { message => 'Get request with already set query' });
$fb->get("$url", { message => 'Get request without already set query' });
$fb->get("$url?limit=1000&access_token=OtherToken", { message => 'Get request with token and query' });
},
server => sub {
my $port = shift;
my $server = Plack::Loader->auto(
port => $port,
host => '127.0.0.1',
);
$server->run($app);
},
);
done_testing(8);
__END__
Net-Facebook-Oauth2-0.09/t/Net-Facebook-Oauth2.t 0000644 0000000 0000000 00000007631 12504331154 017567 0 ustar root root use warnings;
use strict;
use Net::Facebook::Oauth2;
use Test::More;
#########################
#########################
# Fixture Data
my $app_id = 'testapp_id';
my $app_secret = 'test_app_secret';
my $access_token = 'test_access_token';
my $url = 'test.www.com';
my $class = 'Net::Facebook::Oauth2';
eval "use Test::Requires qw/Test::Exception Test::MockObject Test::MockModule/";
if ($@){
plan skip_all => 'Test::Requires required for testing';
} else {
#had to re use??!
use Test::Exception;
use Test::MockObject;
use Test::MockModule;
can_instantiate_class();
test_get_method_with_no_browser_parameter();
can_pass_browser_param();
can_do_delete_request();
done_testing();
}
sub can_instantiate_class {
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
);
ok $net_fb_oauth2,
"Can instantiate $class with application_id and application_secret";
dies_ok { $class->new( application_id => $app_id ) }
'Dies if no application_secret passed to constructor';
dies_ok { $class->new( application_secret => $app_secret ) }
'Dies if no application_id passed to constructor';
}
sub test_get_method_with_no_browser_parameter {
# Test that browser attribute is LWP::UserAgent if no browser param passed
my $test_json = '{"data":"this is the get data"}';
my $mock_get_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
# Mock LWP::UserAgent methods so can test offline
my $mock_user_agent = _mock_object(
{
get => $mock_get_response,
}
);
my $mock_user_agent_module = new Test::MockModule('LWP::UserAgent');
$mock_user_agent_module->mock( 'new', sub {return $mock_user_agent;} );
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
);
is $net_fb_oauth2->get( $url )->as_json, $test_json,
'Passing no browser param will use LWP::UserAgent';
}
sub can_pass_browser_param {
my $test_json = '{"data":"this is the get data"}';
my $mock_get_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
my $mock_browser = _mock_object( {
get => $mock_get_response,
}
);
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
browser => $mock_browser,
);
is $net_fb_oauth2->get( $url )->as_json, $test_json,
'Can pass browser param';
}
sub can_do_delete_request {
my $test_json = '{"data":"this is the delete data"}';
my $mock_delete_response = _mock_object(
{
is_success => 1,
content => $test_json,
}
);
# Mock LWP::UserAgent methods so can test offline
my $mock_user_agent = _mock_object(
{
delete => $mock_delete_response,
}
);
my $mock_user_agent_module = new Test::MockModule('LWP::UserAgent');
$mock_user_agent_module->mock( 'new', sub {return $mock_user_agent;} );
my $net_fb_oauth2 = $class->new(
application_id => $app_id,
application_secret => $app_secret,
access_token => $access_token,
);
is $net_fb_oauth2->delete( $url )->as_json, $test_json,
'Delete request returns correct JSON';
}
sub _mock_object {
my $mock_kv = shift;
my $mock_object = Test::MockObject->new;
while ( my($key, $value) = each %$mock_kv) {
$mock_object->set_always($key, $value);
}
return $mock_object;
}
Net-Facebook-Oauth2-0.09/Changes 0000644 0000000 0000000 00000001475 12504712273 015042 0 ustar root root Revision history for Perl module Net::Facebook::Oauth2.
0.09 2015 2015-03-25
- Code clean (Breno G. de Oliveira)
- Using JSON::MaybeXS instead of deprecated JSON::ANY (Breno G. de Oliveira)
- Update Facebook graph API to version 2.2 (Breno G. de Oliveira)
0.08 2013 2013-10-10
- Bug fix reported by Emad Fanous, adding query to already existing one
- Update Changes File
0.07 2013 2013-08-21
- Updating MANIFEST file to include examples folder
0.06 2013-05-28
- Allow Browser options to pass in by & tests (William Bellamy)
- Changed behavior to access_token & tests by (Takatsugu Shigeta)
0.03 2012 2012-09-01
- Fixed some bugs
- Fixed documentations and examples
- Added more examples
0.02 2010-07-29
- Fixed some methods
- Added better documentations
- Added more examples
Net-Facebook-Oauth2-0.09/README.pod 0000644 0000000 0000000 00000014031 12504711754 015203 0 ustar root root =head1 NAME
Net::Facebook::Oauth2 - a simple Perl wrapper around Facebook OAuth v2.0 protocol
=for html
=head1 SYNOPSIS
Somewhere in your application's login process:
use Net::Facebook::Oauth2;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://yourdomain.com/facebook/callback'
);
# get the authorization URL for your application
my $url = $fb->get_authorization_url(
scope => [ 'public_profile', 'email', 'offline_access', 'publish_stream' ],
display => 'page'
);
Now redirect the user to this C<$url>.
Once the user authorizes your application, Facebook will send him/her back
to your application, on the C link provided above.
Inside that callback route, use the verifier code parameter that Facebook
sends to get the access token:
# param() below is a bogus function. Use whatever your web framework
# provides (e.g. $c->req->param('code'), $cgi->param('code'), etc)
my $code = param('code');
my $access_token = $fb->get_access_token(code => $code);
If you got so far, your user is logged! Save this access token in your
database or session.
Later on you can use it to communicate with Facebook on behalf of this user:
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token
);
my $info = $fb->get(
'https://graph.facebook.com/v2.2/me' # Facebook API URL
);
print $info->as_json;
=head1 DESCRIPTION
Net::Facebook::Oauth2 gives you a way to simply access FaceBook Oauth 2.0 protocol
For more information please see example folder shipped with this Module
=head1 SEE ALSO
For more information about Facebook Oauth 2.0 API
Please Check
L
get/post Facebook Graph API
L
=head1 USAGE
=head2 Cnew( %args )>
Pass args as hash. C<%args> are:
=over 4
=item * C
Your application id as you get from facebook developers platform
when you register your application
=item * C
Your application secret id as you get from facebook developers platform
when you register your application
=back
=head2 C<$fb-Eget_authorization_url( %args )>
Return an Authorization URL for your application, once you receive this
URL redirect user there in order to authorize your application
=over 4
=item * C
['offline_access','publish_stream',...]
Array of Extended permissions as described by facebook Oauth2.0 API
you can get more information about scope/Extended Permission from
http://developers.facebook.com/docs/authentication/permissions
Please note that requesting information other than C,
C and C B
=item * C
callback URL, where facebook will send users after they authorize
your application
=item * C
How to display Facebook Authorization page
=over 4
=item * C
This will display facebook authorization page as full page
=item * C
This option is useful if you want to popup authorization page
as this option tell facebook to reduce the size of the authorization page
=item * C
From the name, for wab and mobile applications this option is the best
facebook authorization page will fit there :)
=back
=back
=head2 C<$fb-Eget_access_token( %args )>
Returns access_token string
One arg to pass
=over 4
=item * C
This is the verifier code that facebook send back to your
callback URL once user authorize your app, you need to capture
this code and pass to this method in order to get access_token
Verifier code will be presented with your callback URL as code
parameter as the following
http://your-call-back-url.com?code=234er7y6fdgjdssgfsd...
When access token is returned you need to save it in a secure
place in order to use it later in your application
=back
=head2 C<$fb-Eget( $url,$args )>
Send get request to facebook and returns response back from facebook
=over 4
=item * C
Facebook Graph API URL as string
=item * C<$args>
hashref of parameters to be sent with graph API URL if required
=back
The response returned can be formatted as the following
=over 4
=item * C<$responseEas_json>
Returns response as json object
=item * C<$responseEas_hash>
Returns response as perl hashref
=back
For more information about facebook grapg API, please check
http://developers.facebook.com/docs/api
=head2 C<$fb-Epost( $url,$args )>
Send post request to facebook API, usually to post something
=over 4
=item * C
Facebook Graph API URL as string
=item * C<$args>
hashref of parameters to be sent with graph API URL
=back
For more information about facebook grapg API, please check
http://developers.facebook.com/docs/api
=head1 INSTALLATION
cpanm Net::Facebook::Oauth2
Or the old-fashioned manual way:
perl Makefile.PL
make
make test
make install
=head1 AUTHOR
Mahmoud A. Mehyar, Emamod.mehyar@gmail.comE
=head1 CONTRIBUTORS
Big Thanks To
=over 4
=item * Takatsugu Shigeta L<@comewalk|https://github.com/comewalk>
=item * Breno G. de Oliveira L<@garu|https://github.com/garu>
=item * squinker L<@squinker|https://github.com/squinker>
=item * Valcho Nedelchev L<@valchonedelchev|https://github.com/valchonedelchev>
=back
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012-2015 by Mahmoud A. Mehyar
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.1 or,
at your option, any later version of Perl 5 you may have available.
=cut
Net-Facebook-Oauth2-0.09/META.json 0000644 0000000 0000000 00000002646 12504712664 015175 0 ustar root root {
"abstract" : "a simple Perl wrapper around Facebook OAuth v2.0 protocol",
"author" : [
"Mahmoud A. Mehyar "
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.143240",
"license" : [
"unknown"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "Net-Facebook-Oauth2",
"no_index" : {
"directory" : [
"t",
"inc"
]
},
"prereqs" : {
"build" : {
"requires" : {
"Test::Exception" : "0",
"Test::MockModule" : "0",
"Test::MockObject" : "0",
"Test::More" : "0.88"
}
},
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {
"Carp" : "0",
"JSON::MaybeXS" : "0",
"LWP::UserAgent" : "0",
"URI" : "0",
"URI::Escape" : "0"
}
}
},
"release_status" : "stable",
"resources" : {
"bugtracker" : {
"web" : "https://github.com/mamod/Net-Facebook-Oauth2/issues"
},
"license" : [
"http://dev.perl.org/licenses/"
],
"repository" : {
"url" : "https://github.com/mamod/Net-Facebook-Oauth2"
}
},
"version" : "0.09"
}
Net-Facebook-Oauth2-0.09/MANIFEST 0000644 0000000 0000000 00000000542 12504712664 014676 0 ustar root root Changes
examples/Catalyst/Facebook.pm
examples/CGI/facebook.pl
lib/Net/Facebook/Oauth2.pm
Makefile.PL
MANIFEST
MANIFEST.SKIP
README.pod
t/accesstoken.t
t/Net-Facebook-Oauth2.t
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
Net-Facebook-Oauth2-0.09/MANIFEST.SKIP 0000644 0000000 0000000 00000000155 12504331154 015432 0 ustar root root MYMETA.json
MYMETA.yml
pm_to_blib
blib
Makefile$
\.*tar.gz$
\.tmp$
\.old$
\.bak$
\.git.+
\.travis
Net-Facebook-Oauth2-0.09/Makefile.PL 0000644 0000000 0000000 00000002271 12504331154 015507 0 ustar root root use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'Net::Facebook::Oauth2',
VERSION_FROM => 'lib/Net/Facebook/Oauth2.pm', # finds $VERSION
PREREQ_PM => {
'LWP::UserAgent' => 0,
'URI' => 0,
'Carp' => 0,
'URI::Escape' => 0,
'JSON::MaybeXS' => 0,
},
BUILD_REQUIRES => {
'Test::More' => 0.88,
'Test::Exception' => 0,
'Test::MockObject' => 0,
'Test::MockModule' => 0,
},
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(
ABSTRACT_FROM => 'lib/Net/Facebook/Oauth2.pm', # retrieve abstract from module
AUTHOR => 'Mahmoud A. Mehyar '
)
: ()
),
META_MERGE => {
resources => {
license => 'http://dev.perl.org/licenses/',
bugtracker => 'https://github.com/mamod/Net-Facebook-Oauth2/issues',
repository => 'https://github.com/mamod/Net-Facebook-Oauth2',
},
},
);
Net-Facebook-Oauth2-0.09/lib/ 0000744 0000000 0000000 00000000000 12504712661 014305 5 ustar root root Net-Facebook-Oauth2-0.09/lib/Net/ 0000744 0000000 0000000 00000000000 12504712661 015033 5 ustar root root Net-Facebook-Oauth2-0.09/lib/Net/Facebook/ 0000744 0000000 0000000 00000000000 12504712661 016544 5 ustar root root Net-Facebook-Oauth2-0.09/lib/Net/Facebook/Oauth2.pm 0000644 0000000 0000000 00000026221 12504711720 020244 0 ustar root root package Net::Facebook::Oauth2;
use strict;
use warnings;
use LWP::UserAgent;
use URI;
use URI::Escape;
use JSON::MaybeXS;
use Carp;
use constant ACCESS_TOKEN_URL => 'https://graph.facebook.com/v2.2/oauth/access_token';
use constant AUTHORIZE_URL => 'https://www.facebook.com/v2.2/dialog/oauth';
our $VERSION = '0.09';
sub new {
my ($class,%options) = @_;
my $self = {};
$self->{options} = \%options;
if (!$options{access_token}){
croak "You must provide your application id in new()\nNet::Facebook::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
croak "You must provide your application secret in new()\nNet::Facebook::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret};
}
$self->{browser} = $options{browser} || LWP::UserAgent->new;
$self->{access_token_url} = $options{access_token_url} || ACCESS_TOKEN_URL;
$self->{authorize_url} = $options{authorize_url} || AUTHORIZE_URL;
$self->{access_token} = $options{access_token};
$self->{display} = $options{display} || 'page'; ##other values popup and wab
return bless($self, $class);
}
sub get_authorization_url {
my ($self,%params) = @_;
$params{callback} ||= $self->{options}->{callback};
croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback};
$params{display} = $self->{display} unless defined $params{display};
$self->{options}->{callback} = $params{callback};
my $scope = join(",", @{$params{scope}}) if defined($params{scope});
my $url = $self->{authorize_url}
."?client_id="
.uri_escape($self->{options}->{application_id})
."&redirect_uri="
.uri_escape($params{callback});
$url .= "&scope=$scope" if $scope;
$url .= "&display=".$params{display};
return $url;
}
sub get_access_token {
my ($self,%params) = @_;
$params{callback} ||= $self->{options}->{callback};
$params{code} ||= $self->{options}->{code};
croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code};
croak "You must pass callback URL" unless defined $params{callback};
$self->{options}->{code} = $params{code};
###generating access token URL
my $getURL = $self->{access_token_url}
."?client_id="
.uri_escape($self->{options}->{application_id})
."&redirect_uri="
.uri_escape($params{callback})
."&client_secret="
.uri_escape($self->{options}->{application_secret})
."&code=$params{code}";
my $response = $self->{browser}->get($getURL);
##got an error response from facebook
##die and display error message
if (!$response->is_success){
my $error = decode_json($response->content());
croak "'" .$error->{error}->{type}. "'" . " " .$error->{error}->{message};
}
##everything is ok proccess response and extract access token
my $file = $response->content();
my ($access_token,$expires) = split(/&/, $file);
my ($string,$token) = split(/=/, $access_token);
###save access token
if ($token){
$self->{access_token} = $token;
return $token;
}
croak "can't get access token";
}
sub get {
my ($self,$url,$params) = @_;
unless ($self->_has_access_token($url)) {
croak "You must pass access_token" unless defined $self->{access_token};
$url .= $self->{_has_query} ? '&' : '?';
$url .= "access_token=" . $self->{access_token};
}
##construct the new url
my @array;
while ( my ($key, $value) = each(%{$params})){
$value = uri_escape($value);
push(@array, "$key=$value");
}
my $string = join('&', @array);
$url .= "&".$string if $string;
my $response = $self->{browser}->get($url);
my $content = $response->content();
return $self->_content($content);
}
sub post {
my ($self,$url,$params) = @_;
unless ($self->_has_access_token($url)) {
croak "You must pass access_token" unless defined $self->{access_token};
$params->{access_token} = $self->{access_token};
}
my $response = $self->{browser}->post($url,$params);
my $content = $response->content();
return $self->_content($content);
}
sub delete {
my ($self,$url,$params) = @_;
unless ($self->_has_access_token($url)) {
croak "You must pass access_token" unless defined $self->{access_token};
$params->{access_token} = $self->{access_token};
}
my $response = $self->{browser}->delete($url,$params);
my $content = $response->content();
return $self->_content($content);
}
sub as_hash {
my ($self) = @_;
return decode_json($self->{content});
}
sub as_json {
my ($self) = @_;
return $self->{content};
}
sub _content {
my ($self,$content) = @_;
$self->{content} = $content;
return $self;
}
sub _has_access_token {
my ($self, $url) = @_;
my $uri = URI->new($url);
my %q = $uri->query_form;
#also check if we have a query and save result
$self->{_has_query} = $uri->query();
if (grep { $_ eq 'access_token' } keys %q) {
return 1;
}
return;
}
1;
__END__
=head1 NAME
Net::Facebook::Oauth2 - a simple Perl wrapper around Facebook OAuth v2.0 protocol
=for html
=head1 SYNOPSIS
Somewhere in your application's login process:
use Net::Facebook::Oauth2;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://yourdomain.com/facebook/callback'
);
# get the authorization URL for your application
my $url = $fb->get_authorization_url(
scope => [ 'public_profile', 'email', 'offline_access', 'publish_stream' ],
display => 'page'
);
Now redirect the user to this C<$url>.
Once the user authorizes your application, Facebook will send him/her back
to your application, on the C link provided above.
Inside that callback route, use the verifier code parameter that Facebook
sends to get the access token:
# param() below is a bogus function. Use whatever your web framework
# provides (e.g. $c->req->param('code'), $cgi->param('code'), etc)
my $code = param('code');
my $access_token = $fb->get_access_token(code => $code);
If you got so far, your user is logged! Save this access token in your
database or session.
Later on you can use it to communicate with Facebook on behalf of this user:
my $fb = Net::Facebook::Oauth2->new(
access_token => $access_token
);
my $info = $fb->get(
'https://graph.facebook.com/v2.2/me' # Facebook API URL
);
print $info->as_json;
=head1 DESCRIPTION
Net::Facebook::Oauth2 gives you a way to simply access FaceBook Oauth 2.0 protocol
For more information please see example folder shipped with this Module
=head1 SEE ALSO
For more information about Facebook Oauth 2.0 API
Please Check
L
get/post Facebook Graph API
L
=head1 USAGE
=head2 Cnew( %args )>
Pass args as hash. C<%args> are:
=over 4
=item * C
Your application id as you get from facebook developers platform
when you register your application
=item * C
Your application secret id as you get from facebook developers platform
when you register your application
=back
=head2 C<$fb-Eget_authorization_url( %args )>
Return an Authorization URL for your application, once you receive this
URL redirect user there in order to authorize your application
=over 4
=item * C
['offline_access','publish_stream',...]
Array of Extended permissions as described by facebook Oauth2.0 API
you can get more information about scope/Extended Permission from
L
Please note that requesting information other than C,
C and C B
=item * C
callback URL, where facebook will send users after they authorize
your application
=item * C
How to display Facebook Authorization page
=over 4
=item * C
This will display facebook authorization page as full page
=item * C
This option is useful if you want to popup authorization page
as this option tell facebook to reduce the size of the authorization page
=item * C
From the name, for wab and mobile applications this option is the best
facebook authorization page will fit there :)
=back
=back
=head2 C<$fb-Eget_access_token( %args )>
Returns access_token string
One arg to pass
=over 4
=item * C
This is the verifier code that facebook send back to your
callback URL once user authorize your app, you need to capture
this code and pass to this method in order to get access_token
Verifier code will be presented with your callback URL as code
parameter as the following
http://your-call-back-url.com?code=234er7y6fdgjdssgfsd...
When access token is returned you need to save it in a secure
place in order to use it later in your application
=back
=head2 C<$fb-Eget( $url,$args )>
Send get request to facebook and returns response back from facebook
=over 4
=item * C
Facebook Graph API URL as string
=item * C<$args>
hashref of parameters to be sent with graph API URL if required
=back
The response returned can be formatted as the following
=over 4
=item * C<$responseEas_json>
Returns response as json object
=item * C<$responseEas_hash>
Returns response as perl hashref
=back
For more information about facebook grapg API, please check
http://developers.facebook.com/docs/api
=head2 C<$fb-Epost( $url,$args )>
Send post request to facebook API, usually to post something
=over 4
=item * C
Facebook Graph API URL as string
=item * C<$args>
hashref of parameters to be sent with graph API URL
=back
For more information about facebook grapg API, please check
L
=head1 AUTHOR
Mahmoud A. Mehyar, Emamod.mehyar@gmail.comE
=head1 CONTRIBUTORS
Big Thanks To
=over 4
=item * Takatsugu Shigeta L<@comewalk|https://github.com/comewalk>
=item * Breno G. de Oliveira L<@garu|https://github.com/garu>
=item * squinker L<@squinker|https://github.com/squinker>
=item * Valcho Nedelchev L<@valchonedelchev|https://github.com/valchonedelchev>
=back
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012-2015 by Mahmoud A. Mehyar
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.1 or,
at your option, any later version of Perl 5 you may have available.
=cut
Net-Facebook-Oauth2-0.09/META.yml 0000644 0000000 0000000 00000001530 12504712662 015012 0 ustar root root ---
abstract: 'a simple Perl wrapper around Facebook OAuth v2.0 protocol'
author:
- 'Mahmoud A. Mehyar '
build_requires:
Test::Exception: '0'
Test::MockModule: '0'
Test::MockObject: '0'
Test::More: '0.88'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.04, CPAN::Meta::Converter version 2.143240'
license: unknown
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: Net-Facebook-Oauth2
no_index:
directory:
- t
- inc
requires:
Carp: '0'
JSON::MaybeXS: '0'
LWP::UserAgent: '0'
URI: '0'
URI::Escape: '0'
resources:
bugtracker: https://github.com/mamod/Net-Facebook-Oauth2/issues
license: http://dev.perl.org/licenses/
repository: https://github.com/mamod/Net-Facebook-Oauth2
version: '0.09'