Facebook SDK - 无法获取会话 [英] Facebook SDK - Unable to get session

查看:161
本文介绍了Facebook SDK - 无法获取会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter制作画布应用程式(这很重要),而不是网路应用程式。据我所知,这意味着我不需要像传统的Facebook Connect应用程序那样将用户登录到一个登录URL上。 Facebook应该处理。

I'm using CodeIgniter to make a canvas app (this is important), NOT a web app. To my understanding that means I don't have to log the user in like a traditional Facebook Connect app would, by forwarding them onto a Login URL. Facebook should handle that.

然而,虽然我得到了一个完整的签名请求,我无法获得一个会话。下面是我的代码:

However, although I get a full signed request, I'm unable to get a session. Below is my code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookCanvasLoginHelper;

class Welcome extends CI_Controller {

    public function index()
    {
        FacebookSession::setDefaultApplication('303962883111629', '71b94393839fc34d2cfd43006791c5ca');

        $helper = new FacebookCanvasLoginHelper();
        try {
            $session = $helper->getSession();
            echo '<pre>';
            print_r($helper);
            echo '</pre>';
        } catch(FacebookRequestException $e) {
            // When Facebook returns an error
            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        } catch(\Exception $e) {
            // When validation fails or other local issues
            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }

        if ($session) {
            // Logged in
            $me = (new FacebookRequest(
                $session, 'GET', '/me'
            ))->execute()->getGraphObject(GraphUser::className());

            echo '<pre>$me: '.print_r($me, true).'</pre>';
        }
    }
}

由于显而易见的原因删除的值):

This outputs the following (some values removed for obvious reasons):

Facebook\FacebookCanvasLoginHelper Object
(
    [signedRequest:protected] => Facebook\Entities\SignedRequest Object
        (
            [rawSignedRequest] => FmHcCuoY8lW9MXgqu5jQYrdzXQAhSC1GtCdPzeX**[partially-removed]**XNlciI6eyJjb3VudHJ5IjoiZ2IiLCJsb2NhbGUiOiJlbl9HQiIsImFnZSI6eyJtaW4iOjIxfX19
            [payload] => Array
                (
                    [algorithm] => HMAC-SHA256
                    [issued_at] => 1405023802
                    [user] => Array
                        (
                            [country] => gb
                            [locale] => en_GB
                            [age] => Array
                                (
                                    [min] => 21
                                )

                        )

                )

        )

    [appId:protected] => **[removed]**
    [appSecret:protected] => **[removed]**
    [state] => 
)

$ session

推荐答案

在Canvas Facebook应用程序中,您需要请求用户授权您的应用程序恢复他们的

In a Canvas Facebook app, you need to ask the user to authorize your application to recover their unique identifier (and some other stuff).

所有你可以得到的是用户的语言,如果他喜欢或不喜欢你的页面Canvas应用程序位于。就是这样。

All you can get without that is the langage of the user, and if he likes or not the page where you Canvas app is located. That's it.

是的,您需要他们登录到您的应用程序,然后您将能够访问其唯一标识符。否则你将总是得到一个NULL答案。

So yes, you need them to log in to your application, and then you will be able to access their unique identifier. Otherwise you will always get a NULL answer.

我通常使用他们的Javascript SDK来启动画布应用程序的登录弹出窗口,以便用户保持在同一个窗口。

I generaly use their Javascript SDK to fire the login popup in the canvas app, so that the user stays in the same windows.

我希望这会对您有所帮助!

I hope this will help you !

这篇关于Facebook SDK - 无法获取会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆