我有一个使用facebook登录的网站的个人资料页面。当页面第一次加载时,登录工作正常,一切似乎都正常。然而,当刷新配置文件页面时,我会收到一个错误:"图形返回错误:此授权代码已被使用。"通过进行一些研究,我了解到这可能是因为我需要使用当前会话?我在脸书上搜索了开发者,但不知道如何做到这一点。我还研究了其他一些有类似错误消息的页面,但当页面刷新时,似乎没有一个页面覆盖。我使用Javascript SDK进行登录过程,使用PHP SDK进行图形请求和登录后的任何操作。我正在使用的代码以及sql查询如下:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
ini_set("display_errors", 1);
// error reporting is now turned on
require('facebook-sdk-v5/autoload.php');
$fb = new FacebookFacebook([
'app_id' => 'app-id-here',
'app_secret' => 'app-secret-here',
'default_graph_version' => 'v2.2',
]);
# fb-login-callback
$jsHelper = $fb->getJavaScriptHelper();
// @TODO This is going away soon
$facebookClient = $fb->getClient();
try {
$accessToken = $jsHelper->getAccessToken($facebookClient);
} catch(FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage(); /*****This is where the error is caught*****/
} catch(FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
try {
// Returns a `FacebookFacebookResponse` object
$_SESSION['facebook_access_token'] = (string) $accessToken;
$response = $fb->get('/me?fields=id,name,email,gender,hometown', $_SESSION['facebook_access_token']);
} catch(FacebookExceptionsFacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
//Check the database for the user
//First connect
require_once 'config.php';
try {
$mysqli= new mysqli($host, $username, $password, $dbname);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
}
catch (mysqli_sql_exception $e) {
throw $e;
}
$stmt = $mysqli->prepare( "SELECT COUNT(*) FROM users WHERE fbuid = ?");
$stmt->bind_param("i", $id);
//Returns a `FacebookGraphNodesGraphUser` collection
$user = $response->getGraphUser();
$id = $user['id'];
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($count);
$stmt->fetch();
//If this is a new user, insert into the users table
if($count === 0) {
$stmt->free_result();
$stmt = $mysqli->prepare("INSERT INTO users (fbuid, name, email, location, profpic, gender, joined)
VALUES ( ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("issssss", $id, $newName, $newEmail, $newLoc, $newPic, $newGender, $joinDate);
//Get the date for the joinDate
$date=getdate(date("U"));
//Get the required information from the Graph API
$locationObj = $user['hometown'];
$joinDate = "$date[month] $date[mday], $date[year]";
$newName = $user['name'];
$newEmail = $user['email'];
$newLoc = $locationObj['name'];
$newPic = "http://graph.facebook.com/" . $id . "/picture?type=large";
$newGender = $user['gender'];
$stmt->execute();
}
//Retrieve their info from the users table
$stmt->free_result();
$stmt = $mysqli->prepare("SELECT name, email, location, profpic, gender, joined, about FROM users WHERE fbuid = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($name, $email, $location, $profpic, $gender, $joined, $about);
$stmt->fetch();
}
else {
// Unable to read JavaScript SDK cookie
}
?>
我的问题是,如何更改我提供的代码以防止出现此错误。如果必须使用当前会话,我该如何检查和使用它。非常感谢。
在PHP脚本开始时使用session_start();
,然后检查if(!isset($_SESSION['fbuid']))
,如果为true(未设置),则调用API图,在第一次调用该图后,将$_SESSION['fbuid']
设置为从调用中检索到的facebook用户id,并将所有用户信息插入MySQL中的用户表中。然后在页面刷新时,它识别正在设置的会话变量,跳过API调用,只从数据库中获取与$_SESSION['fbuid']
匹配的facebook UID的用户的信息