2012-02-21 33 views
2

我的代碼:Facebook的getLoginUrl和下一個參數不能正常工作

$url = $fb->getLoginUrl(array('scope'=>'email','next'=>'http://apps.facebook.com/APPID/')); 
echo "<script> top.location=\"".$url."\"; </script>"; 

我需要將用戶重定向到我的應用程序的應用程序URL時authetication是全成,但它總是重定向到我的網頁REDIRECT_URI。

我該如何解決它?

感謝。

回答

8

您必須更改此URL以在認證後將您的應用重定向到您想要的位置。

You have to change this URL to redirect you app where you want after authentication.

或者你可以做到這一點

首先,你不必編輯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>"); 
    } 

?> 

如果你想獲得更多的權限,然後簡單地將另一個「範圍」參數添加到登錄網址,例如:

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

謝謝它工作正常:) – user964104 2012-02-22 00:38:47

1

更改應用程序設置頁面中的重定向uri。

+0

將其更改爲'http://apps.facebook.com/APPID/'?你的意思是? – user964104 2012-02-21 23:14:11

+0

如果鏈接已在應用程序頁面中設置,則Mike會將「next」值更改爲您希望指向的位置。 – 2012-02-21 23:19:29

+0

哈哈?我這樣做 – user964104 2012-02-21 23:22:29