我想通过 Graph API(使用 Perl)获取 FB 用户数据。
我有一个 facebook 应用程序,配置为“带有 FB 登录的网站”。
我正在使用Net::Facebook::Oauth2
应用程序配置了一个回调URL,如下所示:“http://localhost/myfile.pl”
当我打开 localhost/myfile.pl 时,它可以让我登录 Facebook,并允许应用程序访问我的数据。但是,当它应该回退并获取访问 token 时(至少我认为这就是它下一步应该做的事情),它会以无限循环结束。
http://localhost/myfile.pl
包含以下内容:
#!"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;
我在 Perl 脚本中做错了什么吗?或者是因为我的回调的本地主机?
提前致谢,
克里斯托夫·特罗迪
最佳答案
您应该使用参数来阻止无限循环的发生。基本上,应用程序对 FB 进行身份验证,然后再次调用相同的脚本,它会看到它已通过身份验证并永远循环。
if ( ! defined $cgi->param('code') ){
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
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);
} else {
##later on your application you can use this verifier code to comunicate
##with facebook on behalf of this user
my $access_token = $fb->get_access_token(code => $cgi->param('code'));
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;
}
关于facebook - Perl - 身份验证图 API 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579320/