blib/lib/Net/Facebook/Oauth2.pm

filter: Statementstmt 59.4% 82 / 138 Branchbran 33.3% 26 / 78 Conditioncond 9.3% 3 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 73.6% 14 / 19 Podpod 80.0% 8 / 10 totaltotal 44.6% 133 / 298 SCAR 61.9
CC
51
Cov
45%
get_authorization_url
90.0
get_access_token
72.0
debug_token
72.0
CRAP 489.5
↑ Summary
1 package Net::Facebook::Oauth2;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.11';
12  
13 sub new {
14 7 ▶ my ($class,%options) = @_;
Subroutine new: called CC=13 SCAR=30.9
Pod documented
15 7 my $self = {};
16 7 $self->{options} = \%options;
17  
18 7 ▶ my $api_version = defined $options{api_version} ? $options{api_version} : 'v3.1';
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'api_version'} ? : 0 7 7
19  
20 7 ▶ if (!defined $options{access_token}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"access_token"}) 3 4 7
21 3 ▶ croak "You must provide your application id in new()\nNet::Facebook::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_id"} 1 2 3
22 2 ▶ croak "You must provide your application secret in new()\nNet::Facebook::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_secret"} 1 1 2
23 }
24  
25 5 ▶ if (defined $options{access_token_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'access_token_url'}) { } 0 5 5
26 0 ▶ croak "cannot pass access_token_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
27 0 $self->{access_token_url} = $options{access_token_url};
28 }
29 else {
30 5 $self->{access_token_url} = "https://graph.facebook.com/$api_version/oauth/access_token";
31 }
32  
33 5 ▶ if (defined $options{authorize_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'authorize_url'}) { } 0 5 5
34 0 ▶ croak "cannot pass authorize_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
35 0 $self->{authorize_url} = $options{authorize_url};
36 }
37 else {
38 5 $self->{authorize_url} = "https://www.facebook.com/$api_version/dialog/oauth";
39 }
40  
41 5 ▶ if (defined $options{debug_token_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'debug_token_url'}) { } 0 5 5
42 0 ▶ croak "cannot pass debug_token_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
43 0 $self->{debug_token_url} = $options{debug_token_url};
44 }
45 else {
46 5 $self->{debug_token_url} = "https://graph.facebook.com/$api_version/debug_token";
47 }
48  
49 5 ▶ $self->{browser} = $options{browser} || LWP::UserAgent->new;
Condition66%2/3
$options{'browser'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
140
MC/DC0%0/2
$options{'browser'} || 'LWP::UserAgent'->new
$options{'browser'} 'LWP::UserAgent'->new
Truth table66%2/3
A || B
A= $options{'browser'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
50 5 ▶ $self->{display} = $options{display} || 'page'; ## other values popup and wab
Condition50%1/2
$options{'display'} || 'page'
l!l
05
MC/DC0%0/1
$options{'display'} || 'page'
$options{'display'}
Truth table50%1/2
A
A= $options{'display'}
Aresult
11
00
51 5 $self->{access_token} = $options{access_token};
52  
53 5 return bless($self, $class);
54 }
55  
56 sub get_authorization_url {
57 0 ▶ my ($self,%params) = @_;
Subroutine get_authorization_url: not called CC=9 SCAR=45.0
Pod documented
58  
59 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
60 0 ▶ croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
61  
62 0 ▶ $params{display} = $self->{display} unless defined $params{display};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"display"} 0 0 0
63 0 $self->{options}->{callback} = $params{callback};
64  
65 my $url = $self->{authorize_url}
66 ."?client_id="
67 .uri_escape($self->{options}->{application_id})
68 ."&redirect_uri="
69 0 .uri_escape($params{callback});
70  
71 0 ▶ if ($params{scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"scope"}) 0 0 0
72 0 my $scope = join(',', @{$params{scope}});
73 0 ▶ $url .= '&scope=' . $scope if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 0 0 0
74 }
75 # state is now required:
76 0 ▶ $url .= '&state=' . (defined $params{state} ? $params{state} : time);
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'state'} ? : 0 0 0
77  
78 0 ▶ $url .= '&response_type=' . $params{response_type} if $params{response_type};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"response_type"} 0 0 0
79 0 ▶ $url .= '&auth_type=' . $params{auth_type} if $params{auth_type};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"auth_type"} 0 0 0
80 0 $url .= "&display=".$params{display};
81  
82 0 return $url;
83 }
84  
85  
86 sub get_access_token {
87 0 ▶ my ($self,%params) = @_;
Subroutine get_access_token: not called CC=8 SCAR=42.8
Pod documented
88 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
89 0 ▶ $params{code} ||= $self->{options}->{code};
Condition0%0/3
$params{"code"} ||= $self->{"options"}{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"code"} ||= $self->{"options"}{"code"}
$params{"code"} $self->{"options"}{"code"}
Truth table0%0/3
A ||= B
A= $params{"code"}B= $self->{"options"}{"code"}
ABresult
1X1
011
000
90  
91 0 ▶ croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"code"} 0 0 0
92 0 ▶ croak "You must pass callback URL" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
93 0 $self->{options}->{code} = $params{code};
94  
95 ###generating access token URL
96 my $getURL = $self->{access_token_url}
97 ."?client_id="
98 .uri_escape($self->{options}->{application_id})
99 ."&redirect_uri="
100 .uri_escape($params{callback})
101 ."&client_secret="
102 .uri_escape($self->{options}->{application_secret})
103 0 ."&code=$params{code}";
104  
105 0 my $response = $self->{browser}->get($getURL);
106 0 my $json = decode_json($response->content());
107  
108 0 ▶ if (!$response->is_success || exists $json->{error}){
Branch: 2 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif ($json->{'access_token'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
109 ##got an error response from facebook. die and display error message
110 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
111 }
112 elsif ($json->{access_token}) {
113 ##everything is ok proccess response and extract access token
114 0 return $self->{access_token} = $json->{access_token};
115 }
116 else {
117 0 croak "can't get access token from " . $response->content();
118 }
119 }
120  
121 sub get_long_lived_token {
122 0 ▶ my ($self,%params) = @_;
Subroutine get_long_lived_token: not called CC=7 SCAR=40.3
Pod documented
123  
124 0 ▶ if (!$self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"access_token"}) 0 0 0
125 0 ▶ croak "You must pass the access_token" unless defined $params{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"access_token"} 0 0 0
126 }
127  
128 my $getURL = $self->{access_token_url}
129 . '?grant_type=fb_exchange_token'
130 . '&client_id=' . uri_escape($self->{options}->{application_id})
131 . '&client_secret=' . uri_escape($self->{options}->{application_secret})
132 . '&fb_exchange_token=' . uri_escape($self->{access_token} || $params{access_token})
133 0 ▶ ;
Condition0%0/3
$self->{'access_token'} || $params{'access_token'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'access_token'} || $params{'access_token'}
$self->{'access_token'} $params{'access_token'}
Truth table0%0/3
A || B
A= $self->{'access_token'}B= $params{'access_token'}
ABresult
1X1
011
000
134 0 my $response = $self->{browser}->get($getURL);
135 0 my $json = decode_json($response->content());
136 0 ▶ if (!$response->is_success || exists $json->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif ($json->{'access_token'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
137 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
138 }
139 elsif ($json->{access_token}) {
140 0 return $self->{access_token} = $json->{access_token};
141 }
142 else {
143 0 croak "can't get long lived access token from " . $response->content();
144 }
145 }
146  
147 sub debug_token {
148 0 ▶ my ($self,%params) = @_;
Subroutine debug_token: not called CC=8 SCAR=42.8
Pod documented
149 croak "You must pass the 'input' access token to inspect"
150 0 ▶ unless defined $params{input};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"input"} 0 0 0
151  
152 # NOTE: according to FB's documentation, instead of passing an
153 # app token we can simply pass access_token=app_id|app_secret:
154 # https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens
155 my $getURL = $self->{debug_token_url}
156 . "?input_token=" . uri_escape($params{input})
157 . "&access_token="
158 . join('|',
159 uri_escape($self->{options}->{application_id}),
160 uri_escape($self->{options}->{application_secret})
161 0 );
162  
163 0 my $response = $self->{browser}->get($getURL);
164 0 my $json = decode_json($response->content());
165 0 ▶ if (!$response->is_success || exists $json->{error}){
Branch: 3 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif (not exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}) { } 0 0 0
elsif (not $params{'skip_check'} and '' . $json->{'data'}{'app_id'} ne '' . $self->{'options'}{'application_id'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
166 ##got an error response from facebook. die and display error message
167 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
168 }
169 ▶ elsif (!exists $json->{data}->{app_id} || !exists $json->{data}->{user_id}) {
Condition0%0/3
exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}
exists $json->{'data'}{'app_id'} exists $json->{'data'}{'user_id'}
Truth table0%0/3
A && B
A= exists $json->{'data'}{'app_id'}B= exists $json->{'data'}{'user_id'}
ABresult
0X0
100
111
170 0 return;
171 }
172 ▶ elsif (!$params{skip_check} && (''.$json->{data}->{app_id}) ne (''.$self->{options}->{application_id}) ) {
Condition0%0/3
not $params{"skip_check"} and '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $params{"skip_check"} and '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
not $params{"skip_check"} '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
Truth table0%0/3
not(A) and B
A= not $params{"skip_check"}B= '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
ABresult
0X0
100
111
173 0 return;
174 }
175 0 return $json->{data};
176 }
177  
178  
179 sub get {
180 7 ▶ my ($self,$url,$params) = @_;
Subroutine get: called CC=6 SCAR=17.9
Pod documented
181 7 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 5 2 7
182 5 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 5 5
183 5 ▶ $url .= $self->{_has_query} ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_has_query'} ? : 1 4 5
184 5 $url .= "access_token=" . $self->{access_token};
185 }
186  
187 ##construct the new url
188 7 my @array;
189  
190 7 ▶ while ( my ($key, $value) = each(%{$params})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 5 7 12
191 5 $value = uri_escape($value);
192 5 push(@array, "$key=$value");
193 }
194  
195 7 my $string = join('&', @array);
196 7 ▶ $url .= "&".$string if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 5 2 7
197  
198 7 my $response = $self->{browser}->get($url);
199 7 my $content = $response->content();
200 7 return $self->_content($content);
201 }
202  
203 sub post {
204 2 ▶ my ($self,$url,$params) = @_;
Subroutine post: called CC=3 SCAR=11.0
Pod documented
205 2 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 1 1 2
206 1 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 1 1
207 1 $params->{access_token} = $self->{access_token};
208 }
209 2 my $response = $self->{browser}->post($url,$params);
210 2 my $content = $response->content();
211 2 return $self->_content($content);
212 }
213  
214 sub delete {
215 1 ▶ my ($self,$url,$params) = @_;
Subroutine delete: called CC=3 SCAR=11.2
Pod documented
216 1 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 1 0 1
217 1 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 1 1
218 1 $params->{access_token} = $self->{access_token};
219 }
220 1 my $response = $self->{browser}->delete($url,$params);
221 1 my $content = $response->content();
222 1 return $self->_content($content);
223 }
224  
225 sub as_hash {
226 0 ▶ my ($self) = @_;
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
227 0 return decode_json($self->{content});
228 }
229  
230 sub as_json {
231 3 ▶ my ($self) = @_;
Subroutine as_json: called CC=1 SCAR=0.0
Pod undocumented
232 3 return $self->{content};
233 }
234  
235 sub _content {
236 10 ▶ my ($self,$content) = @_;
Subroutine _content: called CC=1 SCAR=0.0
237 10 $self->{content} = $content;
238 10 return $self;
239 }
240  
241 sub _has_access_token {
242 10 ▶ my ($self, $url) = @_;
Subroutine _has_access_token: called CC=2 SCAR=6.9
243 10 my $uri = URI->new($url);
244 10 my %q = $uri->query_form;
245 #also check if we have a query and save result
246 10 $self->{_has_query} = $uri->query();
247 10 ▶ if (grep { $_ eq 'access_token' } keys %q) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "access_token";} keys %q) 3 7 10
248 3 return 1;
249 }
250 7 return;
251 }
252  
253 2 1;
254 __END__
255 =head1 NAME
256  
257 Net::Facebook::Oauth2 - a simple Perl wrapper around Facebook OAuth 2.0 protocol
258  
259 =for html
260 <a href="https://travis-ci.org/mamod/Net-Facebook-Oauth2"><img src="https://travis-ci.org/mamod/Net-Facebook-Oauth2.svg?branch=master"></a>
261  
262 =head1 FACEBOOK GRAPH API VERSION
263  
264 This module complies to Facebook Graph API version 3.1, the latest
265 at the time of publication, B<< scheduled for deprecation not sooner than July 26th, 2020 >>.
266  
267 =head1 SYNOPSIS
268  
269 Somewhere in your application's login process:
270  
271 use Net::Facebook::Oauth2;
272  
273 my $fb = Net::Facebook::Oauth2->new(
274 application_id => 'your_application_id',
275 application_secret => 'your_application_secret',
276 callback => 'http://yourdomain.com/facebook/callback'
277 );
278  
279 # get the authorization URL for your application
280 my $url = $fb->get_authorization_url(
281 scope => [ 'email' ],
282 display => 'page'
283 );
284  
285 Now redirect the user to this C<$url>.
286  
287 Once the user authorizes your application, Facebook will send him/her back
288 to your application, on the C<callback> link provided above. PLEASE NOTE
289 THAT YOU MUST PRE-AUTHORIZE YOUR CALLBACK URI ON FACEBOOK'S APP DASHBOARD.
290  
291 Inside that callback route, use the verifier code parameter that Facebook
292 sends to get the access token:
293  
294 # param() below is a bogus function. Use whatever your web framework
295 # provides (e.g. $c->req->param('code'), $cgi->param('code'), etc)
296 my $code = param('code');
297  
298 use Try::Tiny; # or eval {}, or whatever
299  
300 my ($unique_id, $access_token);
301 try {
302 $access_token = $fb->get_access_token(code => $code); # <-- could die!
303  
304 # Facebook tokens last ~2h, but you may upgrade them to ~60d if you want:
305 $access_token = $fb->get_long_lived_token( access_token => $access_token );
306  
307 my $access_data = $fb->debug_token( input => $access_token );
308 if ($access_data && $access_data->{is_valid}) {
309 $unique_id = $access_data->{user_id};
310 # you could also check here for what scopes were granted to you
311 # by inspecting $access_data->{scopes}->@*
312 }
313 } catch {
314 # handle errors here!
315 };
316  
317 If you got so far, your user is logged! Save the access token in your
318 database or session. As shown in the example above, Facebook also provides
319 a unique I<user_id> for this token so you can associate it with a particular
320 user of your app.
321  
322 Later on you can use that access token to communicate with Facebook on behalf
323 of this user:
324  
325 my $fb = Net::Facebook::Oauth2->new(
326 access_token => $access_token
327 );
328  
329 my $info = $fb->get(
330 'https://graph.facebook.com/v3.1/me' # Facebook API URL
331 );
332  
333 print $info->as_json;
334  
335 NOTE: if you skipped the call to C<debug_token()> you can still find the
336 unique user id value with a call to the 'me' endpoint shown above, under
337 C<< $info->{id} >>
338  
339 =head1 DESCRIPTION
340  
341 Net::Facebook::Oauth2 gives you a way to simply access FaceBook Oauth 2.0
342 protocol.
343  
344 The example folder contains some snippets you can look at, or for more
345 information just keep reading :)
346  
347 =head1 SEE ALSO
348  
349 For more information about Facebook Oauth 2.0 API
350  
351 Please Check
352 L<http://developers.facebook.com/docs/>
353  
354 get/post Facebook Graph API
355 L<http://developers.facebook.com/docs/api>
356  
357 =head1 USAGE
358  
359 =head2 C<Net::Facebook::Oauth-E<gt>new( %args )>
360  
361 Returns a new object to handle user authentication.
362 Pass arguments as a hash. The following arguments are I<REQUIRED>
363 unless you're passing an access_token (see optional arguments below):
364  
365 =over 4
366  
367 =item * C<application_id>
368  
369 Your application id as you get from facebook developers platform
370 when you register your application
371  
372 =item * C<application_secret>
373  
374 Your application secret id as you get from facebook developers platform
375 when you register your application
376  
377 =back
378  
379 The following arguments are I<OPTIONAL>:
380  
381 =over 4
382  
383 =item * C<access_token>
384  
385 If you want to instantiate an object to an existing access token, you may
386 do so by passing it to this argument.
387  
388 =item * C<browser>
389  
390 The user agent that will handle requests to Facebook's API. Defaults to
391 LWP::UserAgent, but can be any method that implements the methods C<get>,
392 C<post> and C<delete> and whose response to such methods implements
393 C<is_success> and C<content>.
394  
395 =item * C<display>
396  
397 See C<display> under the C<get_authorization_url> method below.
398  
399 =item * C<api_version>
400  
401 Use this to replace the API version on all endpoints. The default
402 value is 'v3.1'. Note that defining an api_version parameter together with
403 C<authorize_url>, C<access_token_url> or C<debug_token_url> is a fatal error.
404  
405 =item * C<authorize_url>
406  
407 Overrides the default (3.1) API endpoint for Facebook's oauth.
408 Used mostly for testing new versions.
409  
410 =item * C<access_token_url>
411  
412 Overrides the default (3.1) API endpoint for Facebook's access token.
413 Used mostly for testing new versions.
414  
415 =item * C<debug_token_url>
416  
417 Overrides the default (3.1) API endpoint for Facebook's token information.
418 Used mostly for testing new versions.
419  
420 =back
421  
422 =head2 C<$fb-E<gt>get_authorization_url( %args )>
423  
424 Returns an authorization URL for your application. Once you receive this
425 URL, redirect your user there in order to authorize your application.
426  
427 The following argument is I<REQUIRED>:
428  
429 =over 4
430  
431 =item * C<callback>
432  
433 callback => 'http://example.com/login/facebook/success'
434  
435 The callback URL, where Facebook will send users after they authorize
436 your application. YOU MUST CONFIRM THIS URL ON FACEBOOK'S APP DASHBOARD.
437  
438 To do that, go to the App Dashboard, click Facebook Login in the right-hand
439 menu, and check the B<Valid OAuth redirect URIs> in the Client OAuth Settings
440 section.
441  
442 =back
443  
444 This method also accepts the following I<OPTIONAL> arguments:
445  
446 =over 4
447  
448 =item * C<scope>
449  
450 scope => ['user_birthday','user_friends', ...]
451  
452 Array of Extended permissions as described by the Facebook Oauth API.
453 You can get more information about scope/Extended Permission from
454  
455 L<https://developers.facebook.com/docs/facebook-login/permissions/>
456  
457 Please note that requesting information other than C<name>, C<email> and
458 C<profile_picture> B<will require your app to be reviewed by Facebook!>
459  
460 =item * C<state>
461  
462 state => '123456abcde'
463  
464 An arbitrary unique string provided by you to guard against Cross-site Request
465 Forgery. This value will be returned to you by Facebook, unchanged. Note that,
466 as of Facebook API v3.0, this argument is I<mandatory>, so if you don't
467 provide a 'state' argument, we will default to C<time()>.
468  
469 =item * C<auth_type>
470  
471 When a user declines a given permission, you must reauthorize them. But when
472 you do so, any previously declined permissions will not be asked again by
473 Facebook. Set this argument to C<'rerequest'> to explicitly tell the dialog
474 you're re-asking for a declined permission.
475  
476 =item * C<display>
477  
478 display => 'page'
479  
480 How to display Facebook Authorization page. Defaults to C<page>.
481 Can be any of the following:
482  
483 =over 4
484  
485 =item * C<page>
486  
487 This will display facebook authorization page as full page
488  
489 =item * C<popup>
490  
491 This option is useful if you want to popup authorization page
492 as this option tell facebook to reduce the size of the authorization page
493  
494 =item * C<wab>
495  
496 From the name, for wab and mobile applications this option is the best, as
497 the facebook authorization page will fit there :)
498  
499 =back
500  
501 =item * C<response_type>
502  
503 response_type => 'code'
504  
505 When the redirect back to the app occurs, determines whether the response
506 data is in URL parameters or fragments. Defaults to C<code>, which is
507 Facebook's default and useful for cases where the server handles the token
508 (which is most likely why you are using this module), but can be also be
509 C<token>, C<code%20token>, or C<granted_scopes>. Note that changing this to
510 anything other than 'code' might change the login flow described in this
511 documentation, rendering calls to C<get_access_token()> pointless.
512 Please see
513 L<< Facebook's login documentation|https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow >>
514 for more information.
515  
516 =back
517  
518 =head2 C<$fb-E<gt>get_access_token( %args )>
519  
520 This method issues a GET request to Facebook's API to retrieve the
521 access token string for the specified code (passed as an argument).
522  
523 Returns the access token string or raises an exception in case of errors
524 (B<make sure to trap calls with eval blocks or a try/catch module>). Note
525 that Facebook's access tokens are short-lived, around 2h of idle time
526 before expiring. If you want to "upgrade" the token to a long lived one
527 (with around 60 days of idle time), use this token to feed the
528 C<get_long_lived_token()> method.
529  
530 You should call this method inside the route for the callback URI defined
531 in the C<get_authorization_url> method. It receives the following arguments:
532  
533 =over 4
534  
535 =item * C<code>
536  
537 This is the verifier code that Facebook sends back to your
538 callback URL once user authorize your app, you need to capture
539 this code and pass to this method in order to get the access token.
540  
541 Verifier code will be presented with your callback URL as code
542 parameter as the following:
543  
544 http://your-call-back-url.com?code=234er7y6fdgjdssgfsd...
545  
546 Note that if you have fiddled with the C<response_type> argument,
547 you might not get this parameter properly.
548  
549 =back
550  
551 When the access token is returned you need to save it in a secure
552 place in order to use it later in your application. The token indicates
553 that a user has authorized your site/app, meaning you can associate that
554 token to that user and issue API requests to Facebook on their behalf.
555  
556 To know I<WHICH> user has granted you the authorization (e.g. when building
557 a login system to associate that token with a unique user on your database),
558 you must make a request to fetch Facebook's own unique identifier for that
559 user, and then associate your own user's unique id to Facebook's.
560  
561 This was usually done by making a GET request to the C<me> API endpoint and
562 looking for the 'id' field. However, Facebook has introduced a new endpoint
563 for that flow that returns the id (this time as 'user_id') and some extra
564 validation data, like whether the token is valid, to which app it refers to,
565 what scopes the user agreed to, etc, so now you are encouraged to call the
566 C<debug_token()> method as shown in the SYNOPSIS.
567  
568 B<IMPORTANT:> Expect that the length of all access token types will change
569 over time as Facebook makes changes to what is stored in them and how they
570 are encoded. You can expect that they will grow and shrink over time.
571 Please use a variable length data type without a specific maximum size to
572 store access tokens.
573  
574 =head2 C<$fb-E<gt>get_long_lived_token( access_token =E<gt> $access_token )
575  
576 Asks facebook to retrieve the long-lived (~60d) version of the provided
577 short-lived (~2h) access token retrieved from C<get_access_token()>. If
578 successful, this method will return the long-lived token, which you can
579 use to replace the short-lived one. Otherwise, it croaks with an error
580 message, in which case you can continue to use the short-lived version.
581  
582 L<See here|https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing>
583 for the gory details.
584  
585 =head2 C<$fb-E<gt>debug_token( input =E<gt> $access_token )>
586  
587 This method should be called right after C<get_access_token()>. It will
588 query Facebook for details about the given access token and validate that
589 it was indeed granted to your app (and not someone else's).
590  
591 It requires a single argument, C<input>, containing the access code obtained
592 from calling C<get_access_token>.
593  
594 It croaks on HTTP/connection/Facebook errors, returns nothing if for whatever
595 reason the response is invalid without errors (e.g. no app_id and no user_id),
596 and also if the returned app_id is not the same as your own application_id
597 (pass a true value to C<skip_check> to skip this validation).
598  
599 If all goes well, it returns a hashref with the JSON structure returned by
600 Facebook.
601  
602  
603 =head2 C<$fb-E<gt>get( $url, $args )>
604  
605 Sends a GET request to Facebook and stores the response in the given object.
606  
607 =over 4
608  
609 =item * C<url>
610  
611 Facebook Graph API URL as string. You must provide the full URL.
612  
613 =item * C<$args>
614  
615 hashref of parameters to be sent with graph API URL if required.
616  
617 =back
618  
619 You can access the response using the following methods:
620  
621 =over 4
622  
623 =item * C<$responseE<gt>as_json>
624  
625 Returns response as json object
626  
627 =item * C<$responseE<gt>as_hash>
628  
629 Returns response as perl hashref
630  
631 =back
632  
633 For more information about facebook graph API, please check
634 http://developers.facebook.com/docs/api
635  
636 =head2 C<$fb-E<gt>post( $url, $args )>
637  
638 Send a POST request to Facebook and stores the response in the given object.
639 See the C<as_hash> and C<as_json> methods above for how to retrieve the
640 response.
641  
642 =over 4
643  
644 =item * C<url>
645  
646 Facebook Graph API URL as string
647  
648 =item * C<$args>
649  
650 hashref of parameters to be sent with graph API URL
651  
652 =back
653  
654 For more information about facebook graph API, please check
655 L<http://developers.facebook.com/docs/api>
656  
657 =head2 C<$fb-E<gt>delete( $url, $args )>
658  
659 Send a DELETE request to Facebook and stores the response in the given object.
660 See the C<as_hash> and C<as_json> methods above for how to retrieve the
661 response.
662  
663 =over 4
664  
665 =item * C<url>
666  
667 Facebook Graph API URL as string
668  
669 =item * C<$args>
670  
671 hashref of parameters to be sent with graph API URL
672  
673 =back
674  
675 =head1 AUTHOR
676  
677 Mahmoud A. Mehyar, E<lt>mamod.mehyar@gmail.comE<gt>
678  
679 =head1 CONTRIBUTORS
680  
681 Big Thanks To
682  
683 =over 4
684  
685 =item * Takatsugu Shigeta L<@comewalk|https://github.com/comewalk>
686  
687 =item * Breno G. de Oliveira L<@garu|https://github.com/garu>
688  
689 =item * squinker L<@squinker|https://github.com/squinker>
690  
691 =item * Valcho Nedelchev L<@valchonedelchev|https://github.com/valchonedelchev>
692  
693 =back
694  
695 =head1 COPYRIGHT AND LICENSE
696  
697 Copyright (C) 2012-2016 by Mahmoud A. Mehyar
698  
699 This library is free software; you can redistribute it and/or modify
700 it under the same terms as Perl itself, either Perl version 5.10.1 or,
701 at your option, any later version of Perl 5 you may have available.
702  
703 =cut
↑ Summary