/usr/share/perl5/Net/Facebook/Oauth2.pm is in libnet-facebook-oauth2-perl 0.08-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | package Net::Facebook::Oauth2;
use strict;
use warnings;
use LWP::UserAgent;
use URI;
use URI::Escape;
use JSON::Any;
use Carp;
use constant ACCESS_TOKEN_URL => 'https://graph.facebook.com/oauth/access_token';
use constant AUTHORIZE_URL => 'https://www.facebook.com/dialog/oauth';
our $VERSION = '0.08';
sub new {
my ($class,%options) = @_;
my $self = {};
$self->{options} = \%options;
if (!$options{access_token}){
croak "Yuo must provide your application id when construct new method\n Net::Facebook::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
croak "Yuo must provide your application secret when construct new method\n Net::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 $j = JSON::Any->new;
my $error = $j->jsonToObj($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) = @_;
my $j = JSON::Any->new;
return $j->jsonToObj($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;
=head1 NAME
Net::Facebook::Oauth2 - a simple Perl wrapper around Facebook OAuth v2.0 protocol
=head1 SYNOPSIS
use CGI;
my $cgi = CGI->new;
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 authorization URL for your application
my $url = $fb->get_authorization_url(
scope => ['offline_access','publish_stream'],
display => 'page'
);
####now redirect to this url
print $cgi->redirect($url);
##once user authorizes your application facebook will send him/her back to your application
##to the callback link provided above
###in your callback block capture verifier code and get access_token
my $fb = Net::Facebook::Oauth2->new(
application_id => 'your_application_id',
application_secret => 'your_application_secret',
callback => 'http://yourdomain.com/facebook/callback'
);
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
###save this token in database or session
##later on your application you can use this verifier code to comunicate
##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/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<http://developers.facebook.com/docs/>
get/post Facebook Graph API
L<http://developers.facebook.com/docs/api>
=head1 USAGE
=head2 C<Net::Facebook::Oauth-E<gt>new( %args )>
Pass args as hash. C<%args> are:
=over 4
=item * C<application_id >
Your application id as you get from facebook developers platform
when you register your application
=item * C<application_secret>
Your application secret id as you get from facebook developers platform
when you register your application
=back
=head2 C<$fb-E<gt>get_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<scope>
['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<http://developers.facebook.com/docs/authentication/permissions>
=item * C<callback>
callback URL, where facebook will send users after they authorize
your application
=item * C<display>
How to display Facebook Authorization page
=over 4
=item * C<page>
This will display facebook authorization page as full page
=item * C<popup>
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<wab>
From the name, for wab and mobile applications this option is the best
facebook authorization page will fit there :)
=back
=back
=head2 C<$fb-E<gt>get_access_token( %args )>
Returns access_token string
One arg to pass
=over 4
=item * C<code>
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-E<gt>get( $url,$args )>
Send get request to facebook and returns response back from facebook
=over 4
=item * C<url>
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<$responseE<gt>as_json>
Returns response as json object
=item * C<$responseE<gt>as_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-E<gt>post( $url,$args )>
Send post request to facebook API, usually to post something
=over 4
=item * C<url>
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<http://developers.facebook.com/docs/api>
=head1 AUTHOR
Mahmoud A. Mehyar, E<lt>mamod.mehyar@gmail.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012-2013 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
|