2011-07-21 37 views
5

我正在嘗試更改我的網站上Facebook登錄的重定向網址。這樣我就可以進入一個頁面,在那裏我可以在數據庫中創建一個新用戶,如果它們不存在,那麼將它們重定向到我的主頁面。然而,當我嘗試登錄,我得到在Facebook上出現以下錯誤:An error occurred with PHP SDK Unit Tests. Please try again later.登錄時更改Facebook重定向網址

我的代碼(我想重定向他們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();以供參考

回答

5

您重定向到的URL需要與您在應用設置中爲應用配置的域位於同一域中,除此之外沒有任何限制,您可以將redirect_url設置爲任意您網域上的網址。

當您嘗試權威性應用程序作爲管理員應該有更具體的錯誤信息可見 - 它很可能錯誤191(參見Facebook API error 191另一個可能的原因和解決方案)

+0

這就行了,豬。那會做。 – tnw

7

首先,你不」噸有編輯PHP SDK,下面是你更換樣品認證用戶,然後重定向到您的着陸頁,

確保:

YOUR-APP-ID,在這裏與您的Facebook應用程序ID,

YOUR-APP-API的祕密在這裏與您的Facebook應用密鑰

您重定向的URL,在這裏與您的着陸頁網址

<?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>"); 
    } 

?> 

如果你想獲得更多的權限,那麼只需添加另一個「範圍」參數登錄URL,例如:

$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => 'comma-separated-list-of-requested-extended-perms')); 

有關權限的更多詳細信息,請參閱facebook permissions docs

+0

我很欣賞你進入的細節,upvoted。但是,我能夠讓我的原代碼工作,這對我來說已經夠簡單了,謝謝。 – tnw

+0

@Tory Waterman:謝謝,我很感激,沒有問題。 –