File Coverage

blib/lib/Net/Facebook/Oauth2.pm
Criterion Covered Total %
statement 83 114 72.8
branch 21 48 43.7
condition 6 24 25.0
subroutine 17 20 85.0
pod 6 8 75.0
total 133 214 62.1


line stmt bran cond sub pod time code
1             package Net::Facebook::Oauth2;
2            
3 2     2   14650 use strict;
  2         3  
  2         49  
4 2     2   7 use warnings;
  2         2  
  2         44  
5 2     2   1293 use LWP::UserAgent;
  2         74330  
  2         58  
6 2     2   14 use URI;
  2         3  
  2         38  
7 2     2   6 use URI::Escape;
  2         3  
  2         122  
8 2     2   943 use JSON::MaybeXS;
  2         9442  
  2         107  
9 2     2   10 use Carp;
  2         3  
  2         203  
10            
11             BEGIN {
12 2     2   45 my @time = localtime;
13 2 50 33     54 if ($time[5] >= 118 && $time[4] > 8) {
14 0         0 warn "\n****************************************************************************\n"
15             . "[WARNING] This version of Net::Facebook::Oauth2 uses Facebook Graph API v2.8\n"
16             . "which is SCHEDULED FOR DEPRECATION on 5 October 2018. If this module\n"
17             . "(together with any associated code) is not updated, it may stop working!\n"
18             . "****************************************************************************\n"
19             ;
20             }
21             };
22            
23 2     2   10 use constant ACCESS_TOKEN_URL => 'https://graph.facebook.com/v2.8/oauth/access_token';
  2         3  
  2         127  
24 2     2   9 use constant AUTHORIZE_URL => 'https://www.facebook.com/v2.8/dialog/oauth';
  2         10  
  2         1974  
25            
26             our $VERSION = '0.10';
27            
28             sub new {
29 7     7 1 311601 my ($class,%options) = @_;
30 7         22 my $self = {};
31 7         22 $self->{options} = \%options;
32            
33 7 100       34 if (!$options{access_token}){
34 3 100       16 croak "You must provide your application id in new()\nNet::Facebook::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
35 2 100       25 croak "You must provide your application secret in new()\nNet::Facebook::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret};
36             }
37            
38 5   66     65 $self->{browser} = $options{browser} || LWP::UserAgent->new;
39 5   50     5524 $self->{access_token_url} = $options{access_token_url} || ACCESS_TOKEN_URL;
40 5   50     32 $self->{authorize_url} = $options{authorize_url} || AUTHORIZE_URL;
41 5   50     29 $self->{display} = $options{display} || 'page'; ## other values popup and wab
42 5         14 $self->{access_token} = $options{access_token};
43            
44 5         19 return bless($self, $class);
45             }
46            
47             sub get_authorization_url {
48 0     0 1 0 my ($self,%params) = @_;
49            
50 0   0     0 $params{callback} ||= $self->{options}->{callback};
51 0 0       0 croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback};
52            
53 0 0       0 $params{display} = $self->{display} unless defined $params{display};
54 0         0 $self->{options}->{callback} = $params{callback};
55            
56             my $url = $self->{authorize_url}
57             ."?client_id="
58             .uri_escape($self->{options}->{application_id})
59             ."&redirect_uri="
60 0         0 .uri_escape($params{callback});
61            
62 0 0       0 if ($params{scope}) {
63 0         0 my $scope = join(',', @{$params{scope}});
  0         0  
64 0 0       0 $url .= '&scope=' . $scope if $scope;
65             }
66 0 0       0 $url .= '&state=' . $params{state} if $params{state};
67 0 0       0 $url .= '&response_type=' . $params{response_type} if $params{response_type};
68 0 0       0 $url .= '&auth_type=' . $params{auth_type} if $params{auth_type};
69            
70 0         0 $url .= "&display=".$params{display};
71            
72 0         0 return $url;
73             }
74            
75            
76             sub get_access_token {
77 0     0 1 0 my ($self,%params) = @_;
78 0   0     0 $params{callback} ||= $self->{options}->{callback};
79 0   0     0 $params{code} ||= $self->{options}->{code};
80            
81 0 0       0 croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code};
82 0 0       0 croak "You must pass callback URL" unless defined $params{callback};
83 0         0 $self->{options}->{code} = $params{code};
84            
85             ###generating access token URL
86             my $getURL = $self->{access_token_url}
87             ."?client_id="
88             .uri_escape($self->{options}->{application_id})
89             ."&redirect_uri="
90             .uri_escape($params{callback})
91             ."&client_secret="
92             .uri_escape($self->{options}->{application_secret})
93 0         0 ."&code=$params{code}";
94            
95 0         0 my $response = $self->{browser}->get($getURL);
96 0         0 my $json = decode_json($response->content());
97            
98 0 0 0     0 if (!$response->is_success || exists $json->{error}){
    0          
99             ##got an error response from facebook. die and display error message
100 0         0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
101             }
102             elsif ($json->{access_token}) {
103             ##everything is ok proccess response and extract access token
104 0         0 return $self->{access_token} = $json->{access_token};
105             }
106             else {
107 0         0 croak "can't get access token from " . $response->content();
108             }
109             }
110            
111             sub get {
112 7     7 1 51 my ($self,$url,$params) = @_;
113 7 100       18 unless ($self->_has_access_token($url)) {
114 5 50       23 croak "You must pass access_token" unless defined $self->{access_token};
115 5 100       24 $url .= $self->{_has_query} ? '&' : '?';
116 5         10 $url .= "access_token=" . $self->{access_token};
117             }
118            
119             ##construct the new url
120 7         9 my @array;
121            
122 7         10 while ( my ($key, $value) = each(%{$params})){
  12         42  
123 5         16 $value = uri_escape($value);
124 5         138 push(@array, "$key=$value");
125             }
126            
127 7         16 my $string = join('&', @array);
128 7 100       19 $url .= "&".$string if $string;
129            
130 7         73 my $response = $self->{browser}->get($url);
131 7         22751 my $content = $response->content();
132 7         132 return $self->_content($content);
133             }
134            
135             sub post {
136 2     2 1 33 my ($self,$url,$params) = @_;
137 2 100       12 unless ($self->_has_access_token($url)) {
138 1 50       4 croak "You must pass access_token" unless defined $self->{access_token};
139 1         3 $params->{access_token} = $self->{access_token};
140             }
141 2         13 my $response = $self->{browser}->post($url,$params);
142 2         400321 my $content = $response->content();
143 2         28 return $self->_content($content);
144             }
145            
146             sub delete {
147 1     1 1 5 my ($self,$url,$params) = @_;
148 1 50       4 unless ($self->_has_access_token($url)) {
149 1 50       4 croak "You must pass access_token" unless defined $self->{access_token};
150 1         3 $params->{access_token} = $self->{access_token};
151             }
152 1         10 my $response = $self->{browser}->delete($url,$params);
153 1         68 my $content = $response->content();
154 1         40 return $self->_content($content);
155             }
156            
157             sub as_hash {
158 0     0 0 0 my ($self) = @_;
159 0         0 return decode_json($self->{content});
160             }
161            
162             sub as_json {
163 3     3 0 3 my ($self) = @_;
164 3         12 return $self->{content};
165             }
166            
167             sub _content {
168 10     10   17 my ($self,$content) = @_;
169 10         19 $self->{content} = $content;
170 10         122 return $self;
171             }
172            
173             sub _has_access_token {
174 10     10   13 my ($self, $url) = @_;
175 10         50 my $uri = URI->new($url);
176 10         10965 my %q = $uri->query_form;
177             #also check if we have a query and save result
178 10         313 $self->{_has_query} = $uri->query();
179 10 100       92 if (grep { $_ eq 'access_token' } keys %q) {
  5         16  
180 3         12 return 1;
181             }
182 7         37 return;
183             }
184            
185             1;
186             __END__