在facebook身份验证后无法打开重定向的页面

时间:2012-06-07 12:31:50

标签: facebook codeigniter facebook-graph-api session-state

我正在使用facebook连接下的codeigniter.after身份验证我想重定向我的控制器的成功方法 这是我的控制器:

class Welcome extends CI_Controller {
function __construct()
{
    parent::__construct();

    $this->load->model('Facebook_model');
}

function index()
{
    $fb_data = $this->session->userdata('fb_data');

    $data = array(
                'fb_data' => $fb_data,
                );

    $this->load->view('welcome', $data);
}

function topsecret()
{
    $fb_data = $this->session->userdata('fb_data');

    if((!$fb_data['uid']) or (!$fb_data['me']))
    {
        redirect('welcome');
    }
    else
    {
        $data = array(
                    'fb_data' => $fb_data,
                    );

        $this->load->view('topsecret', $data);
    }
}
function success()
{
    $this->load->view('welcome_message');
}
}

我的facebook api访问模型:

class Facebook_model extends CI_Model {
public function __construct()
{
    parent::__construct();
    $config = array(
                    'appId'  => '261066574000678',
                    'secret' => '   79e11f65449988965362f58e9a4aabd7',
                    'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
                    );

    $this->load->library('Facebook', $config);

    $user = $this->facebook->getUser();

    // We may or may not have this data based on whether the user is logged in.
    //
    // If we have a $user id here, it means we know the user is logged into
    // Facebook, but we don't know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    $profile = null;
    if($user)
    {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $profile = $this->facebook->api('/me?fields=id,name,link,email');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }       
    }

    $fb_data = array(
                    'me' => $profile,
                    'uid' => $user,
                    'loginUrl' => $this->facebook->getLoginUrl(
                        array(
                            'scope' => 'email,user_birthday,publish_stream', // app permissions
                            'redirect_uri' => 'https://sanjay.localhost.com/index.php/welcome/success' // URL where you want to redirect your users after a successful login
                    )
                    ),
                    'logoutUrl' => $this->facebook->getLogoutUrl(),
                );

    $this->session->set_userdata('fb_data', $fb_data);
}
}

因为我在localhost主机上测试它,我也编辑了我的主机文件,并将我的localhost主机名更改为sanjay.localhost.com.redirect但是没有发生..我认为可能是因为querystring.when重定向发生重定向uri是
=“> HTTPS:?//sanjay.localhost.com/index.php/welcome/success状态= ff5712299510defa&安培;代码= AQCaD-FAd1shuW# =
我不了解如何处理查询字符串中的状态和代码。 请帮忙。

1 个答案:

答案 0 :(得分:0)

感谢您在我的博客上与我联系。首先,Facebook停止了对localhost的支持。她是链接https://developers.facebook.com/bugs/128794873890320

我没有使用codeigniter开发任何应用程序,我使用CakePHP但是auth跟随应该是相同的 1.在用户控制器中创建一个fb_login函数 2.此功能将遵循此逻辑。   一个。使用$facebook->getUser()获取用户ID。   湾然后使用$facebook->api('/me')来确定 3.如果您收到FacebookApiException,请发送用户登录Facebook。如果您使用官方SDK,则会将当前网址添加到重定向网址 4. Facebook将在登录后重定向您的用户。因此您将使用$facebook->getUser()获取数据。将此数据保存在会话中以便在您的应用中进一步使用。然后将用户重定向到您的控制页面或任何其他页面。 CakePHP具有setFlash()功能,可以显示控制面板中设置的msg。我认为Codeignator应该有这样的东西。如果没有,您只需在会话中设置一个消息并重定向用户即可。然后在显示消息后取消设置消息。

这是完整的代码

$uid = $facebook->getUser();        
try {
    $user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) { 
    //echo $e->getMessage();
    $uid = null;
}

$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope'         => 'publish_stream,offline_access,email'
        ),''
);

if (!$uid) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}
//search using uid on your user table to check if the use is returnign user or new user.
if($new_user ==1)
{
    $this->Session->setFlash('please sign up');
    //redirect to sign up
}
else
{
    $this->Session->setFlash('you are good lad');
    //reditect to control panel
}

$uid = $facebook->getUser(); try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { //echo $e->getMessage(); $uid = null; } $loginUrl = $facebook->getLoginUrl( array( 'scope' => 'publish_stream,offline_access,email' ),'' ); if (!$uid) { echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>"; exit; } //search using uid on your user table to check if the use is returnign user or new user. if($new_user ==1) { $this->Session->setFlash('please sign up'); //redirect to sign up } else { $this->Session->setFlash('you are good lad'); //reditect to control panel }