source: perl/modules/Facebook/lib/Facebook/Graph/AccessToken.pm @ c104b43

Last change on this file since c104b43 was c104b43, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Force void context on callbacks. Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
  • Property mode set to 100644
File size: 2.2 KB
Line 
1package Facebook::Graph::AccessToken;
2BEGIN {
3  $Facebook::Graph::AccessToken::VERSION = '1.0300';
4}
5
6use Any::Moose;
7use Facebook::Graph::AccessToken::Response;
8with 'Facebook::Graph::Role::Uri';
9use AnyEvent::HTTP;
10
11has app_id => (
12    is      => 'ro',
13    required=> 1,
14);
15
16has secret => (
17    is      => 'ro',
18    required=> 1,
19);
20
21has postback => (
22    is      => 'ro',
23    required=> 1,
24);
25
26has code => (
27    is      => 'ro',
28    required=> 1,
29);
30
31sub uri_as_string {
32    my ($self) = @_;
33    my $uri = $self->uri;
34    $uri->path('oauth/access_token');
35    $uri->query_form(
36        client_id       => $self->app_id,
37        client_secret   => $self->secret,
38        redirect_uri    => $self->postback,
39        code            => $self->code,
40    );
41    return $uri->as_string;
42}
43
44sub request {
45    my ($self, $cb) = @_;
46    http_get $self->uri_as_string, sub {
47        my ($response, $headers) = @_;
48        $cb->(Facebook::Graph::AccessToken::Response->new(
49            response => $response,
50            headers => $headers,
51            uri => $self->uri_as_string
52        ));
53    };
54    () # return nothing
55}
56
57no Any::Moose;
58__PACKAGE__->meta->make_immutable;
59
60
61=head1 NAME
62
63Facebook::Graph::AccessToken - Acquire an access token from Facebook.
64
65
66=head1 VERSION
67
68version 1.0300
69
70=head1 SYNOPSIS
71
72 my $fb = Facebook::Graph->new(
73    secret      => $facebook_application_secret,
74    app_id      => $facebook_application_id,
75    postback    => 'https://www.yourapplication.com/facebook/postback',
76 );
77 my $token_response_object = $fb->request_access_token($code_from_authorize_postback);
78
79 my $token_string = $token_response_object->token;
80 my $token_expires_epoch = $token_response_object->expires;
81
82=head1 DESCRIPTION
83
84Allows you to request an access token from Facebook so you can make privileged requests on the Graph API.
85
86=head1 METHODS
87
88=head2 uri_as_string ()
89
90Returns the URI that will be called to fetch the token as a string. Mostly useful for debugging and testing.
91
92=head2 request ()
93
94Makes a request to Facebook to fetch an access token. Returns a L<Facebook::Graph::AccessToken::Response> object.
95
96=head1 LEGAL
97
98Facebook::Graph is Copyright 2010 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
99
100=cut
Note: See TracBrowser for help on using the repository browser.