我正在尝试更改我网站上Facebook登录的重定向网址。这样我就可以进入一个页面,我可以在我的数据库中创建一个新用户(如果它们尚不存在),然后将它们重定向到我的主页面。但是,当我尝试登录时,我在Facebook上收到以下错误:An error occurred with PHP SDK Unit Tests. Please try again later.
我的代码(我想将它们重定向到mysite.com/createfbuser.php):
public function getLoginUrl($params=array()) {
$this->establishCSRFTokenState();
$currentUrl = $_SERVER['SERVER_NAME'] . '/createfbuser.php';
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state,
'scope' => 'email'),
$params));
}
编辑原始代码读取$currentUrl = $this->getCurrentUrl();
作为参考
答案 0 :(得分:7)
首先,您不必编辑PHP SDK,下面是验证用户身份然后重定向到目标网页的示例,
确保替换:
你的APP-ID-HERE和你的facebook应用程序ID,
你的APP-API-SECRET-HERE和你的facebook应用程序密钥
您的REDIRECT-URL-HERE与您的目标网页网址
<?php
// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');
define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
// If the user is not connected to your application, redirect the user to authentication page
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
// if the user is already connected, then redirect them to landing page or show some content
echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
}
?>
如果您想获得扩展权限,只需在登录网址中添加另一个“范围”参数,例如:
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => 'comma-separated-list-of-requested-extended-perms'));
有关权限的详细信息,请参阅facebook permissions docs
答案 1 :(得分:5)
您重定向到的网址需要与您在应用设置中为应用配置的网域相同,除此之外没有任何限制,您可以将redirect_url设置为您域中的任意网址。
当您尝试以管理员身份验证应用时,应该会显示更具体的错误消息 - 很可能是错误191(有关其他可能的原因和解决方案,请参阅Facebook API error 191)