【问题标题】:Session issues in cakephp 2.x with facebook sdk in productioncakephp 2.x 中的会话问题与生产中的 facebook sdk
【发布时间】:2016-11-30 10:46:52
【问题描述】:

我正在尝试将 Facebook PHP sdk 与 cakephp 2.x 一起用于登录目的。 它在调试模式 1 或 2 下工作,但在调试模式 0 下不工作。 似乎会话在生产中无法正常工作。 我在网上搜索了很多次,但没有找到适合我的解决方案。

我详细阅读了这两个线程,但没有解决这个问题。 https://github.com/facebook/php-graph-sdk/issues/473 How do I integrate Facebook SDK login with cakephp 2.x?

我在 AppController 中使用这两个函数进行登录。

public function beforeFilter()
{
    $this->disableCache();

    $this->Facebook = new Facebook(array(
        'app_id'                => 'appId',
        'app_secret'            => 'appSecret',
        'default_graph_version' => 'v2.7',
    ));

    $this->Auth->allow(['.....']);
}

public function login()
{
    if (!session_id()) {
        session_start();
    }
    $this->loadModel("User");

    $user_id = $this->Session->read('Auth.User.id');

    $fb          = $this->Facebook->getRedirectLoginHelper();
    $permissions = ['email']; // Optional permissions

    $callback_url = HTTP_ROOT . 'login';
    $fb_login_url = $fb->getLoginUrl($callback_url, $permissions);

    $this->set('fb_login_url', $fb_login_url);

    if (!empty($user_id)) {
        //redirect to profile page if already logged in
        $this->redirect(... . );
    }

    //local login request
    if ($this->request->is('post')) {
        ......
    }

    // when facebook login is used
    elseif ($this->request->query('code')) {
        try {
            $accessToken = $fb->getAccessToken();

        } catch (\Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(), 'error');
            $this->redirect($this->referer());
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            $this->Session->setFlash('Facebook SDK returned an error: ' . $e->getMessage(), 'error');
            $this->redirect($this->referer());
        }

        if (!isset($accessToken)) {
            if ($fb->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                $this->Session->setFlash("Error: " . $fb->getError() . "\n", 'error');
                $this->Session->setFlash("Error Code: " . $fb->getErrorCode() . "\n", 'error');
                $this->Session->setFlash("Error Reason: " . $fb->getErrorReason() . "\n", 'error');
                $this->Session->setFlash("Error Description: " . $fb->getErrorDescription() . "\n", 'error');
                $this->redirect($this->referer());
            } else {
                header('HTTP/1.0 400 Bad Request');
                $this->Session->setFlash('Bad request', 'error');
                $this->redirect($this->referer());
            }
        }

        // Logged in
        $oAuth2Client = $this->Facebook->getOAuth2Client();

        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
        $tokenMetadata->validateAppId('1200125790051089'); // Replace {app-id} with your app id
        $tokenMetadata->validateExpiration();

        if (!$accessToken->isLongLived()) {
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                $this->Session->setFlash('Error getting long-lived access token: ' . $helper->getMessage() . "</p>\n\n", 'error');
                $this->redirect($this->referer());
            }
        }

        $_SESSION['fb_access_token'] = (string) $accessToken;
        $fb_access_token             = (string) $accessToken;

        if (isset($accessToken)) {
            try {
                // Returns a `Facebook\FacebookResponse` object
                $response = $this->Facebook->get('/me?fields=id,first_name,last_name,email', $accessToken);
            } catch (\Facebook\Exceptions\FacebookResponseException $e) {
                $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(), 'error');
                $this->redirect($this->referer());
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                $this->Session->setFlash('Facebook SDK returned an error: ' . $e->getMessage(), 'error');
                $this->redirect($this->referer());
            }

            $fb_user = $response->getGraphUser();

            // We will varify if a local user exists first
            $local_user = $this->User->find('first', array(
                'conditions' => array('facebook_id' => $fb_user['id']),
            ));

            // If exists, we will log them in
            if ($local_user) {
                $this->Auth->login($local_user['User']);
            } else {
                // we will create new user with facebook_id and log them in
                $data['User'] = array(.........);

                // You should change this part to include data validation
                $new_user = $this->User->save($data);
                $this->Auth->login($new_user['User']);
            }
            // redirect to profile page here
        }
    }
}

【问题讨论】:

  • 会话不能正常工作是什么意思?此外,您不需要在登录时手动启动会话,cake 应该会自动处理它。

标签: cakephp facebook-php-sdk


【解决方案1】:

SDK 和 CakePHP 2.x 也有一些问题。我编写了一个小处理程序,让 SDK 可以使用 CakeSession。

你可以在这里找到它:

https://github.com/WrDX/FacebookCakeSessionPersistentDataHandler

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多