我尝试写一个脚本facebook登录。用户php-graph-sdk v5。它在firefox上运行正常但在chrome和其他浏览器上无效。
我的登录页面index.php:
<?php session_start(); ?>
<a class="btn btn-facebook" onclick="login_facebook();" href="javascript:void(0);">facebook login</a>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : 'app_id', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
}
function login_facebook()
{
FB.login(function(response) {
if (response.authResponse)
{
FB.api('/me?fields=id,first_name,last_name,picture,email', function(response) {
var email = response.email;
if(email != '')
{
// alert(email);
window.location.replace('http://localhost/testphp/call-back.php');
}
});
} else
{
console.log('User cancelled login or did not fully authorize.');
}
},{scope: 'email,user_photos'});
}
</script>
我的call-back.php:
<?php
session_start();
require_once('Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => 'app_id', // Replace {app-id} with your app id
'app_secret' => 'app_secret',
'default_graph_version' => 'v2.9',
]);
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
echo $accessToken;
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// Get user’s Facebook ID
$userId = $tokenMetadata->getField('user_id');
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,name,email,first_name,last_name', $accessToken->getValue());
// print_r($response);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'ERROR: Graph ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'ERROR: validation fails ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
//print_r($me);
echo "Full Name: ".$me->getProperty('name')."<br>";
echo "First Name: ".$me->getProperty('first_name')."<br>";
echo "Last Name: ".$me->getProperty('last_name')."<br>";
echo "Email: ".$me->getProperty('email');
echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>";
?>
它在firefox上工作正常,但chrome和其他一些浏览器返回错误。致命错误:在第24行的call-back.php中调用未定义的方法Facebook \ Helpers \ FacebookJavaScriptHelper :: getError()。你能否帮我解决这个错误。谢谢!