问题
I'm stuck... Everything looks correct but it does not work. Tried with a debug access token from my app at facebook developer and it works great so its not an issue with the resto of the code.
index.php
<?php
session_start();
/******Improting Facebook API Files**************/
require_once 'src/Facebook/autoload.php';
require_once 'credentials.php';
/******Facebook API Connection With My APP**************/
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $appsecret,
'default_graph_version' => 'v2.3'
]);
/******Initializing The Login**************/
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'public_profile']; // optional
#echo '<script type="text/javascript">alert("'.$incommingurl.'");</script>';
#echo '<script type="text/javascript">alert("'.$permissions.'");</script>';
#var_dump($permissions);
#var_dump($helper);
$loginUrl = $helper->getLoginUrl($incommingurl, $permissions);
/******Printing The Login URL**************/
echo'<a href="' . $loginUrl . '" class="facebookLoginButton" title="Login With Facebook">Sign in with Facebook</a>';
?>
token.php
<?php
session_start();
/******Improting Facebook API Files**************/
require_once 'src/Facebook/autoload.php';
require_once 'credentials.php';
/******Facebook API Connection With My APP**************/
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $appsecret,
'default_graph_version' => 'v2.3'
]);
$helper = $fb->getRedirectLoginHelper();
/******Getting Token From Facebook**************/
var_dump($helper);
try {
echo '<script type="text/javascript"> alert("1235")</script>';
$accessToken = $helper->getAccessToken();
var_dump($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;
}
/******Storing Token In Sessions For Further Use**************/
if (isset($accessToken)) {
$_SESSION['token'] = (string) $accessToken;
echo $_SESSION['token'];
header('location: collectUserData.php');
}
else{
//echo "err";
header('location: index.php');
}
?>
Setting $accesstoken with a valid token the code works so im not getting the access token. I can dump $helper and the result is:
object(Facebook\Helpers\FacebookRedirectLoginHelper)#7 (4) { ["oAuth2Client":protected]=> object(Facebook\Authentication\OAuth2Client)#8 (4) { ["app":protected]=> object(Facebook\FacebookApp)#3 (2) { ["id":protected]=> string(15) "xxxxxxxxxxxx" ["secret":protected]=> string(32) "xxxxxxxxxxxxxxxxxx" } ["client":protected]=> object(Facebook\FacebookClient)#4 (2) { ["enableBetaMode":protected]=> bool(false) ["httpClientHandler":protected]=> object(Facebook\HttpClients\FacebookCurlHttpClient)#5 (4) { ["curlErrorMessage":protected]=> string(0) "" ["curlErrorCode":protected]=> int(0) ["rawResponse":protected]=> NULL ["facebookCurl":protected]=> object(Facebook\HttpClients\FacebookCurl)#6 (1) { ["curl":protected]=> NULL } } } ["graphVersion":protected]=> string(4) "v2.3" ["lastRequest":protected]=> NULL } ["urlDetectionHandler":protected]=> object(Facebook\Url\FacebookUrlDetectionHandler)#10 (0) { } ["persistentDataHandler":protected]=> object(Facebook\PersistentData\FacebookSessionPersistentDataHandler)#9 (1) { ["sessionPrefix":protected]=> string(6) "FBRLH_" } ["pseudoRandomStringGenerator":protected]=> object(Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator)#11 (0) { } }
But no result on $accessToken = $helper->getAccessToken(); var_dump($accessToken);
Can anyone please help?
Best regards and a happy new year :-)
回答1:
https://github.com/facebook/php-graph-sdk/issues/958
petertoth0507 commented on 25 May 2018
The solution is in the following: In the old times the getAccessToken function worked without parameter: $accessToken = $helper->getAccessToken();
In current time you must set the callback URL, example: $accessToken = $helper->getAccessToken('https://www.classfund.hu/index.php?r=site/fblogincallback');
In parallel you must set the same parameter at https://developers.facebook.com/apps/ page at your application property, Products->Settings->Valid OAuth Redirect URIs field.
When I set the above mentioned params the authentication operating properly again....
来源:https://stackoverflow.com/questions/54031002/facebook-grap-apu-accesstoken-helper-getaccesstoken-with-no-value