Facebook getLoginUrl和下一个参数无法正常工作

时间:2012-02-21 23:06:38

标签: php facebook-graph-api

我的代码:

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

我需要在验证成功时将用户重定向到我的应用程序的应用程序URL,但它总是重定向到我的redirect_uri页面。

我该如何解决?

感谢。

2 个答案:

答案 0 :(得分:8)

您必须更改此网址,以便在身份验证后将应用重定向到您想要的位置。

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

或者你可以这样做

首先,您不必编辑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'));

答案 1 :(得分:1)

在应用设置页面中更改重定向uri。