嗨,我遇到了一个网上找不到解决方案的问题。我正在尝试使用Facebook登录名登录我的网站。这是我的代码。 getAccessToken()方法始终返回null。请帮助我解决此错误。谢谢。下面是我的代码。
这是我的fbConfig.php文件
<?php
if(!session_id()){
session_start();
}
//Include the autoloader provided in the SDK
require_once 'C:\xampp\htdocs\FBFunApp\php-graph-sdk-5.x\src\Facebook\autoload.php';
//Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = '560615054394144'; //Facebook App ID
$appSecret = 'AAAAAAAAAAAAAAAAAAAA'; //Facebook App Secret
$redirectURL = 'http://localhost:8080/FBFunApp/views/login.php'; //Callback URL
$fbPermissions = array('email'); //Optional permissions
$fb = new Facebook(array('app_id' => $appId,'app_secret' => $appSecret,'default_graph_version' => 'v2.10',));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
var_dump($accessToken); //returns null
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}catch(Exception $e){
echo 'Exception: '+$e->getMessage();
exit;
}
?>
这是login.php文件。内部使用配置文件。
<?php
//Include FB config file & User class
require_once '../config/fbConfig.php';
require_once '../controllers/User.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
//Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// Redirect the user back to the same page if url has "code" parameter in query string
if(isset($_GET['code'])){
header('Location: ./');
}
// Getting user facebook profile info
try {
$profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,cover,picture');
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
print_r($fbUserProfile);
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// Redirect user back to app login page
header("Location: ./");
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// Initialize User class
$user = new User();
// Insert or update user data to the database
$fbUserData = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $fbUserProfile['id'],
'first_name' => $fbUserProfile['first_name'],
'last_name' => $fbUserProfile['last_name'],
'email' => $fbUserProfile['email'],
'gender' => $fbUserProfile['gender'],
'locale' => $fbUserProfile['locale'],
'cover' => $fbUserProfile['cover']['source'],
'picture' => $fbUserProfile['picture']['url'],
'link' => $fbUserProfile['link']
);
$userData = $user->checkUser($fbUserData);
// Put user data into session
$_SESSION['userData'] = $userData;
// Get logout url
$logoutURL = $helper->getLogoutUrl($accessToken, $redirectURL.'views/logout.php');
// Render facebook profile data
if(!empty($userData)){
$output = '<h2 style="color:#999999;">Facebook Profile Details</h2>';
$output .= '<div style="position: relative;">';
$output .= '<img src="'.$userData['cover'].'" />';
$output .= '<img style="position: absolute; top: 90%; left: 25%;" src="'.$userData['picture'].'"/>';
$output .= '</div>';
$output .= '<br/>Facebook ID : '.$userData['oauth_uid'];
$output .= '<br/>Name : '.$userData['first_name'].' '.$userData['last_name'];
$output .= '<br/>Email : '.$userData['email'];
$output .= '<br/>Gender : '.$userData['gender'];
$output .= '<br/>Locale : '.$userData['locale'];
$output .= '<br/>Logged in with : Facebook';
$output .= '<br/>Profile Link : <a href="'.$userData['link'].'" target="_blank">Click to visit Facebook page</a>';
$output .= '<br/>Logout from <a href="'.$logoutURL.'">Facebook</a>';
}else{
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
}
}else{
// Get login url
$loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
// Render facebook login button
$output = '<a href="'.htmlspecialchars($loginURL).'"><img src="../images/app/fb-login.png"></a>';
}
?>
<html>
<head>
<title>Login with Facebook</title>
</head>
<body>
<!-- Display login button / Facebook profile information -->
<div><?php echo $output; ?></div>
</body>
</html>
答案 0 :(得分:0)
facebook api更新后:
每个请求网址必须为Https
所以,
更改此:-
$redirectURL = 'http://localhost:8080/FBFunApp/views/login.php'; //Callback URL
收件人:
$redirectURL = 'https://localhost:8080/FBFunApp/views/login.php'; //Callback URL
您可以使用更改网络服务器设置或使用默认端口:-ssl 443来完成此操作。
快乐编码:)