Good afternoon, I'm finishing the login screen with PHP using Facebook API, at first I was able to click on the button I created and it already asked me to release the access on Facebook, this way I already managed to bring: / p>
ID
, Nome
, Picture
of the user that is logging in, however XAMPP is now giving me the following error:
Fatal error: Uncaught InvalidArgumentException: The default access token must be of type "string" or Facebook\AccessToken in H:\Xampp\htdocs\projeto\lib\Facebook\Facebook.php:263 Stack trace: #0 H:\Xampp\htdocs\projeto\face.php(42): Facebook\Facebook->setDefaultAccessToken(Object(__PHP_Incomplete_Class)) #1 {main} thrown in H:\Xampp\htdocs\projeto\lib\Facebook\Facebook.php on line 263
Can anyone help me?
<?php
session_start();
require_once 'lib/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
'app_id' => '-----------------',
'app_secret' => '-----------------------------',
'default_graph_version' => 'v2.10',
//'default_access_token' => '{access-token}', // optional
]);
$helper = $fb->getRedirectLoginHelper();
//var_dump ($helper);
$permissions = ['email']; // Optional permissions
try {
if(isset($_SESSION['face_access_token'])){
$accessToken = $_SESSION['face_access_token'];
}else{
$accessToken = $helper->getAccessToken();
}
} 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)) {
$url_login = 'http://localhost/projeto/face.php';
$loginUrl = $helper->getLoginUrl( $url_login, $permissions);
}else{
$url_login = 'http://localhost/projeto/face.php';
$loginUrl = $helper->getLoginUrl( $url_login, $permissions);
// usuário já autenticado
if(isset($_SESSION['face_access_token'])){
$fb->setDefaultAccessToken($_SESSION['face_access_token']);
}//usuário não autenticado
else{
$_SESSION['face_access_token'] = (string) $accessToken;
$oAuth2Client = $fb->getOAuth2Client();
$_SESSION['face_access_token'] = $oAuth2Client->getLongLivedAccessToken($_SESSION['face_access_token']);
$fb->setDefaultAccessToken($_SESSION['face_access_token']);
}
try {
// Returns a 'Facebook\FacebookResponse' object
$response = $fb->get('/me?fields=name, picture, email');
$user = $response->getGraphUser();
//var_dump($user);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
?>
<a href="<?php echo $loginUrl; ?>">Facebook</a>