I want to get FB-user data via the Graph API (with Perl).
I have an facebook-application, configured as “Website with FB Login”.
I’m using Net::Facebook::Oauth2
the app is configured with a callbackurl like this: "http://localhost/myfile.pl"
When I open localhost/myfile.pl it lets me login to facebook and also give the application access to my data. But when it comes to the point where it should fall back and getting the access-token (at least I think thats what it should do next) it ends in an endless loop.
http://localhost/myfile.pl contains the following:
#!"C:\strawberry\perl\bin\perl.exe"
use CGI::Carp qw(fatalsToBrowser); # this makes perl showing (syntax) errors in browser
use CGI;
my $cgi = CGI->new;
use Net::Facebook::Oauth2;
my $fb = Net::Facebook::Oauth2->new(
application_id => 'xxxMY_APP_IDXXX',
application_secret => 'xxxMY_SECRETxxx',
callback => 'http://localhost/myFile.pl'
);
###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 => 'xxxMY_APP_IDxxx',
application_secret => 'xxxMY_SECRETxxx',
callback => 'http://localhost/myFile.pl'
);
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;
Am I doing something wrong in the Perl script? Or is it because of the localhost for my callback?
Thanks in advance,
Christoph Twrdy
You should use a parameter to stop infinite loops happen. Basicly, the app authtenticate to FB then call the same script again, it sees that it is authtenticated and loops forever.