我正在使用来自Facebook开发者页面的修改示例。我的JS:
window.fbAsyncInit = function() {
FB.init({
appId : '{xxx}',
cookie : true,
xfbml : true,
version : 'v2.8'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.8&appId=xxx";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', {fields: 'id,name,email'}, function(response) {
console.log('Successful login for: ' + response.name);
console.log('Here is email: ' + response.email);
document.getElementById('status').innerHTML ='Thanks for logging in, ' + response.name + '!';
});
$.get(baseurl + 'index.php/auth/fb_login', function(response){
console.log(response);
});
}
$(document).ready(function(){
$('body').on('click', '.fb-login', function(){
FB.login(function(response){
statusChangeCallback(response);
}, {scope: 'public_profile,email'});
});
});
基本思路是,在成功登录后,我在控制台中显示响应对象并运行testAPI()
,这将进行API调用并在控制台中显示名称和电子邮件。之后我添加了对auth/fb_login
的ajax调用:
function fb_login(){
require_once( 'vendor/autoload.php' );
$fb = new Facebook\Facebook([
'app_id' => $this->config->item('app_id', 'tank_auth'),
'app_secret' => $this->config->item('app_secret', 'tank_auth'),
'default_graph_version' => $this->config->item('default_graph_version', 'tank_auth')
]);
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo $e->getMessage();
exit;
}
if ($accessToken) {
var_dump('Successfully logged in!');die();
}
var_dump(' not Successfully logged in!');die();
}
而var_dump
总是转储"而不是......"没有任何FB例外。
我使用facebook debug tool并返回
Error validating access token: This may be because the user logged out or may be due to a system error.
Application ID xxx: MyAppName
User ID
xxx: Kārlis Janisels
User last installed this app via API v2.x
Issued Unknown
Expires 1480410000 (in about an hour)
Valid False
Origin Unknown
Scopes email, public_profile
所以下划线问题 - 我可以在浏览器中获得有效的访问令牌,但不能在服务器中获取。
如果有帮助 - 我在wamp服务器localhost上使用codeigniter框架。