Skip to content

Commit

Permalink
Added request_access_token and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hopkins authored and Adam Hopkins committed May 24, 2014
1 parent f9cd4fc commit 95c28cf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
30 changes: 30 additions & 0 deletions lib/Facebook/Graph.pm
Expand Up @@ -71,6 +71,22 @@ sub request_access_token {
return $token;
}

sub request_extended_access_token {
my ($self, $access_token) = @_;

die "request_extended_access_token requires an access_token" unless $access_token or $self->has_access_token;
$access_token = $access_token ? $access_token : $self->access_token;

my $token = Facebook::Graph::AccessToken->new(
access_token => $access_token,
postback => $self->postback,
secret => $self->secret,
app_id => $self->app_id,
)->request;
$self->access_token($token->token);
return $token;
}

sub convert_sessions {
my ($self, $sessions) = @_;
return Facebook::Graph::Session->new(
Expand Down Expand Up @@ -342,9 +358,17 @@ Handle the Facebook authorization code postback:
my $q = Plack::Request->new($env);
$fb->request_access_token($q->query_param('code'));
#now retrieve extended access token
$fb->request_extended_access_token; #extended access token now in $fb->access_token
Or if you already had the access token:
$fb->access_token($token);
$fb->request_extended_access_token;
Or simply:
$fb->request_extended_access_token($token);
Get some info:
Expand Down Expand Up @@ -403,6 +427,12 @@ Creates a L<Facebook::Graph::Authorize> object, which can be used to get permiss
Creates a L<Facebook::Graph::AccessToken> object and fetches an access token from Facebook, which will allow everything you do with Facebook::Graph to work within user privileges rather than through the public interface. Returns a L<Facebook::Graph::AccessToken::Response> object, and also sets the C<access_token> property in the Facebook::Graph object.
=head2 request_extended_access_token ( access_token )
Note: access_token is optional. Creates a L<Facebook::Graph::AccessToken> object and fetches an (https://developers.facebook.com/docs/facebook-login/access-tokens/#extending) extended access token from Facebook.
This method accepts an optional access token. If you have called C<request_access_token> already on the Facebook::Graph object and C<access_token> is set, then you do not have to pass
in an access token. However, if you have an access token stored from a previous object, you will need to pass it in.
=head3 code
An authorization code string that you should have gotten by going through the C<authorize> process.
Expand Down
39 changes: 32 additions & 7 deletions lib/Facebook/Graph/AccessToken.pm
Expand Up @@ -22,19 +22,44 @@ has postback => (

has code => (
is => 'ro',
required=> 1,
required=> 0,
predicate=> 'has_code',
);

has access_token => (
is => 'ro',
required => 0,
predicate => 'has_access_token',
);

sub BUILD {
my $self = shift;
die "Either code or access_token is required" if not $self->has_code and not $self->has_access_token;
}

sub uri_as_string {
my ($self) = @_;
my $uri = $self->uri;
$uri->path('oauth/access_token');
$uri->query_form(
client_id => $self->app_id,
client_secret => $self->secret,
redirect_uri => $self->postback,
code => $self->code,
);

if($self->has_code) {
$uri->query_form(
client_id => $self->app_id,
client_secret => $self->secret,
redirect_uri => $self->postback,
code => $self->code,
);
}
else {
$uri->query_form(
grant_type => 'fb_exchange_token',
client_id => $self->app_id,
client_secret => $self->secret,
redirect_uri => $self->postback,
fb_exchange_token => $self->access_token,
);
}

return $uri->as_string;
}

Expand Down

0 comments on commit 95c28cf

Please sign in to comment.