facebook SDK登录中的奇怪错误

时间:2017-10-10 15:45:10

标签: facebook facebook-graph-api facebook-php-sdk

我正在寻找一个没有任何结果的解决方案......

我已经为facebook正确安装了PHP库,创建了一个带有ID和密码的应用程序,创建了页面php并工作直到用facebook帐户确认访问,显示此错误:

(回调网址) 图表返回了一个错误:Impossibile caricare l'URL:Il dominio di questo URLnonèincrononei domini dell'app。 Per poter caricare questo URL,aggiungi tutti i domini e sottodomini della tua app al campo Domini app nelle tue impostazioni app。

无法加载网址,因为网址域中缺少网址....但是错误!

这是我的APP配置 enter image description here

This is config php 
------------------------------------
$appId          = '20386911XXXXXXX'; //Facebook App ID
$appSecret      = '1441a5f9dXXXXXXXXXXXXXc03aae06'; //Facebook App Secret
$redirectURL    = 'http_MY_WEB_SITE_/facebook_login_with_php/'; //Callback 
URL
$fbPermissions  = array('email');  //Optional permissions
------------------------------------

这是回调网址的结果:

http://www.makewish.it/facebook_login_with_php/?code=AQBRhvLnJWwNAMIKa5b..(etc)

图表返回错误:URL kann nicht geladen werden:Die Domain dieser URL ist nicht in den Domains der App vorhanden。 Um diese URL ladenzukönnen,fügealleDomains und Subdomains deiner App im Appdomain-Feld in deinen App-Einstellungen hinzu。

在我的有效OAuth中,我有 http_MY_WEB_SITE_ / facebook_login_with_php /

为什么会出现这个问题?我怎么解决?感谢。

1 个答案:

答案 0 :(得分:4)

我完全一样!代码连接到FB登录页面,但在回调中,我在德语语言中收到此错误... ???

过了一段时间,我找到了一个解决方案,我需要在$ accessToken = $ helper-> getAccessToken();

中明确定义重定向网址

所以我改变了:

$ accessToken = $ helper-> getAccessToken();

$ accessToken = $ helper-> getAccessToken(' https://...[my redirect url]')

这解决了我的问题。

这是我的小步骤的完整解决方案(没有代码优化,但只是为了使步骤更容易理解:

登录页面



if(!session_id()){
    session_start();
}

// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/autoload.php';

// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

/*
 * Configuration and setup Facebook SDK
 */
$appId          = 'fill in yours'; //Facebook App ID
$appSecret      = 'fill in yours'; //Facebook App Secret
$redirectURL    = 'https://fill in yours/get-user-back.php'; //Callback URL
$fbPermissions  = array('email');  //Optional permissions

$fb = new Facebook(array(
    'app_id' => $appId,
    'app_secret' => $appSecret,
    'default_graph_version' => 'v2.2',
));

// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();

// Get login url
$loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);

// next make a button with $loginURL


返回页面

echo "Back from FB
";

if(!session_id()){ session_start(); }

// Include the autoloader provided in the SDK require_once DIR . '/facebook-php-sdk/autoload.php';

// Include required libraries use Facebook\Facebook; use Facebook\Exceptions\FacebookResponseException; use Facebook\Exceptions\FacebookSDKException;

/* * Configuration and setup Facebook SDK */ $appId = 'fill in yours'; //Facebook App ID $appSecret = 'fill in yours'; //Facebook App Secret $redirectURL = 'https://fill in yours/get-user-back.php'; //Callback URL $fbPermissions = array('email'); //Optional permissions

$fb = new Facebook(array( 'app_id' => $appId, 'app_secret' => $appSecret, 'default_graph_version' => 'v2.2', ));

// Get redirect login helper $helper = $fb->getRedirectLoginHelper();

try { $accessToken = $helper->getAccessToken('https://fill in yours/get-user-back.php'); } 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; }

if (isset($accessToken)) {

// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

if (! $accessToken->isLongLived()) {
    echo "<HR>accesToken is not LongLived<HR>";
    // Exchanges a short-lived access token for a long-lived one
    try {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
    echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
    exit;
    }   
}

$fb->setDefaultAccessToken($accessToken);

echo "<HR>";

// Getting user facebook profile info
try {
    $profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
    $fbUserProfile = $profileRequest->getGraphNode()->asArray();


} 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;
}

echo '<pre>';print_r($fbUserProfile); echo '</pre>';    

}

// OAuth 2.0 client handler helps to manage access tokens $oAuth2Client = $fb->getOAuth2Client(); if (! $accessToken->isLongLived()) { echo "<HR>accesToken is not LongLived<HR>"; // Exchanges a short-lived access token for a long-lived one try { $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); } catch (Facebook\Exceptions\FacebookSDKException $e) { echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n"; exit; } } $fb->setDefaultAccessToken($accessToken); echo "<HR>"; // Getting user facebook profile info try { $profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture'); $fbUserProfile = $profileRequest->getGraphNode()->asArray(); } 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; } echo '<pre>';print_r($fbUserProfile); echo '</pre>';