问题Facebook PHP SDK登录机制

时间:2014-03-11 14:23:59

标签: php facebook facebook-graph-api facebook-php-sdk

我对Facebook PHP SDK登录机制有一些(理解)问题。

当我尝试使用我的应用设置中找到的静态access_token登录时,一切正常。

但是:我如何从代码参数解析它,这是由登录回调给出的?或者我在哪里动态获取access_token以便发布内容?

我尝试做的是使用我的用户帐户在Facebook页面上发布内容。

我使用静态令牌的代码是:

$redirected = $_GET('redirected');

if (!empty($redirected) && $redirected === 'true')
{
    $callback = $_GET('callback') . '?code=' . $_GET('code');
    header("Location: {$callback}");
    exit(0);
}


class CFacebookClient
{
    // BEGIN CONFIG settings
    protected $sAppId;
    protected $sSecret;
    protected $bFileUpload = false;
    protected $bAllowSignedRequest = false;
    // END CONFIG settings


    protected $facebook;

    protected $sUid;
    protected $sLoginUrl;

    protected $profile;

    protected $sLastError;

    protected $sLoginCallback = 'http://localhost/facebook/test.facebookclient.php?loggedin=true';

    protected $sAuthCode;


    public function __construct($sAppId, $sSecret)
    {
        $this->sAppId = $sAppId;
        $this->sSecret = $sSecret;
    }

    public function setAppid($sAppId)
    {
        $this->sAppId = $sAppId;
    }

    public function setSecret($sSecret)
    {
        $this->sSecret;
    }

    public function setFileUpload($boolean)
    {
        $this->bFileUpload = $boolean;
    }

    public function setAllowSignedRequest($boolean)
    {
        $this->bAllowSignedRequest = $boolean;
    }

    public function getUid()
    {
        return $this->sUid;
    }

    public function getLoginUrl()
    {
        return $this->sLoginUrl;
    }

    public function getProfile()
    {
        return $this->profile;
    }

    public function setAuthCode($sCode)
    {
        $this->sAuthCode = $sCode;
    }

    public function setLoginCallback($sCallbackUri)
    {
        $this->sLoginCallback = $sCallbackUri;
    }

    public function lastError()
    {
        return $this->sLastError;
    }


    public function init()
    {
       $this->facebook = new Facebook(array(
                                            'appId' => $this->sAppId,
                                            'secret' => $this->sSecret,
                                            'fileUpload' => $this->bFileUpload,
                                            'allowSignedRequest' => $this->bAllowSignedRequest,
                                            'cookie' => true
                                                )
                                       );
    }

    public function login()
    {
        $rw = true;
        $this->sUid = $this->facebook->getUser();
        $this->sLoginUrl = $this->facebook->getLoginUrl(array(
                                                              'scope' => 'email,publish_stream,manage_pages',
                                                              'redirect_uri' => $this->sLoginCallback
                                                            )
                                                        );
        if ($this->sUid)
        {
            try
            {
                $this->profile = $this->facebook->api('/me');
            }
            catch (FacebookApiException $fbEx)
            {
                $this->sLastError = $fbEx->getMessage();
                $rw = false;
            }
            return $rw;
        }
        else
        {
            return false;
        }
    }

    public function loginPages()
    {
        $rw = true;
        $this->sUid = $this->facebook->getUser();
        $this->sLoginUrl = $this->facebook->getLoginUrl(array(
                                                              'scope' => 'email,publish_stream,manage_pages',
                                                              'redirect_uri' => $this->sLoginCallback
                                                            )
                                                        );
        if ($this->sUid)
        {
            try
            {
                $this->profile = $this->facebook->api('/me/accounts');
            }
            catch (FacebookApiException $fbEx)
            {
                $this->sLastError = $fbEx->getMessage();
                $rw = false;
            }
            return $rw;
        }
        else
        {
            return false;
        }
    }

    public function retrieveProfile()
    {
        return $this->facebook->api('/me/accounts', 'GET', array('access_token' => $this->sAuthCode));
    }
}

test.facebookclient.php文件:

$sAppId = 'xxxx';
$sSecret = 'xxxx';

$sLoggedin = $_GET('loggedin');
$fb = new CFacebookClient($sAppId, $sSecret);
$fb->init();

if ($sLoggedin === 'true')
{
    $fb->setAuthCode('ddddd');
    //$fb->setAuthCode($_GET('code'));
    var_dump($fb->retrieveProfile());
    exit(0);
}

if (!$fb->loginPages())
{
    header('Location: ' . $fb->getLoginUrl());
    exit(0);
}
else
{
    var_dump($fb->retrieveProfile());
    exit(0);
}
exit(0);

编辑:

解决方法是在/ me / accounts从方法retrieveProfile()

中隐藏api调用之后删除所有内容

0 个答案:

没有答案