File Coverage

blib/lib/Facebook/Graph/AccessToken.pm
Criterion Covered Total %
statement 9 20 45.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 3 6 50.0
pod 2 3 66.6
total 14 36 38.8


line stmt bran cond sub pod time code
1             package Facebook::Graph::AccessToken;
2             $Facebook::Graph::AccessToken::VERSION = '1.1202';
3 4     4   15 use Moo;
  4         6  
  4         17  
4 4     4   2245 use Facebook::Graph::AccessToken::Response;
  4         11  
  4         142  
5 4     4   2240 use Facebook::Graph::Request;
  4         10  
  4         981  
6             with 'Facebook::Graph::Role::Uri';
7              
8             has app_id => (
9             is => 'ro',
10             required=> 1,
11             );
12              
13             has secret => (
14             is => 'ro',
15             required=> 1,
16             );
17              
18             has postback => (
19             is => 'ro',
20             required=> 1,
21             );
22              
23             has code => (
24             is => 'ro',
25             required=> 0,
26             predicate=> 'has_code',
27             );
28              
29             has access_token => (
30             is => 'ro',
31             required => 0,
32             predicate => 'has_access_token',
33             );
34              
35             sub BUILD {
36 0     0 0   my $self = shift;
37 0 0 0       die "Either code or access_token is required" if not $self->has_code and not $self->has_access_token;
38             }
39              
40             sub uri_as_string {
41 0     0 1   my ($self) = @_;
42 0           my $uri = $self->uri;
43 0           $uri->path('oauth/access_token');
44              
45 0 0         if($self->has_code) {
46 0           $uri->query_form(
47             client_id => $self->app_id,
48             client_secret => $self->secret,
49             redirect_uri => $self->postback,
50             code => $self->code,
51             );
52             }
53             else {
54 0           $uri->query_form(
55             grant_type => 'fb_exchange_token',
56             client_id => $self->app_id,
57             client_secret => $self->secret,
58             redirect_uri => $self->postback,
59             fb_exchange_token => $self->access_token,
60             );
61             }
62              
63 0           return $uri->as_string;
64             }
65              
66             sub request {
67 0     0 1   my ($self) = @_;
68 0           return Facebook::Graph::AccessToken::Response->new(
69             response => Facebook::Graph::Request->new->get($self->uri_as_string)->response
70             );
71             }
72              
73             1;
74              
75             =head1 NAME
76              
77             Facebook::Graph::AccessToken - Acquire an access token from Facebook.
78              
79              
80             =head1 VERSION
81              
82             version 1.1202
83              
84             =head1 SYNOPSIS
85              
86             my $fb = Facebook::Graph->new(
87             secret => $facebook_application_secret,
88             app_id => $facebook_application_id,
89             postback => 'https://www.yourapplication.com/facebook/postback',
90             );
91             my $token_response_object = $fb->request_access_token($code_from_authorize_postback);
92              
93             my $token_string = $token_response_object->token;
94             my $token_expires_epoch = $token_response_object->expires;
95              
96             =head1 DESCRIPTION
97              
98             Allows you to request an access token from Facebook so you can make privileged requests on the Graph API.
99              
100             =head1 METHODS
101              
102             =head2 new ( [ params ] )
103              
104             =over
105              
106             =item params
107              
108             A hash or hashref of parameters to pass to the constructor.
109              
110             =over
111              
112             =item app_id
113              
114             The application id that you get from Facebook after registering (L<http://developers.facebook.com/setup/>) your application on their site. Required if you'll be calling the C<request_access_token>, C<convert_sessions>, or C<authorize> methods.
115              
116             =item code
117              
118             An authorization code string that you should have gotten by going through the C<authorize> process.
119              
120             =item postback
121              
122             The URI that Facebook should post your authorization code back to. Required if you'll be calling the C<request_access_token> or C<authorize> methods.
123              
124             =item secret
125              
126             The application secret that you get from Facebook after registering your application. Required if you'll be calling the C<request_access_token> or C<convert_sessions> methods.
127              
128              
129             =back
130              
131             =back
132              
133             =head2 uri_as_string ()
134              
135             Returns the URI that will be called to fetch the token as a string. Mostly useful for debugging and testing.
136              
137             =head2 request ()
138              
139             Makes a request to Facebook to fetch an access token. Returns a L<Facebook::Graph::AccessToken::Response> object.
140              
141             =head2 build ()
142              
143             Checks for either access_token or code and dies if has neither.
144              
145             =head1 LEGAL
146              
147             Facebook::Graph is Copyright 2010 - 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
148              
149             =cut