cakephp 2.x中的会话问题与生产中的facebook sdk
发布时间:2020-12-13 16:06:22 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用Facebook PHP sdk和cakephp 2.x进行登录. 它正在使用调试模式1或2,但它不适用于调试模式0. 似乎会话在生产中不能正常工作. 我在网上多次搜索它,但没有为我找到合适的解决方案. 我详细阅读了这两个主题,但没有解决问题. https://github.com/fac
我正在尝试使用Facebook
PHP sdk和cakephp 2.x进行登录.
它正在使用调试模式1或2,但它不适用于调试模式0. 似乎会话在生产中不能正常工作. 我在网上多次搜索它,但没有为我找到合适的解决方案. 我详细阅读了这两个主题,但没有解决问题. 我在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 (FacebookExceptionsFacebookResponseException $e) { // When Graph returns an error $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(),'error'); $this->redirect($this->referer()); } catch (FacebookExceptionsFacebookSDKException $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 (FacebookExceptionsFacebookSDKException $e) { $this->Session->setFlash('Error getting long-lived access token: ' . $helper->getMessage() . "</p>nn",'error'); $this->redirect($this->referer()); } } $_SESSION['fb_access_token'] = (string) $accessToken; $fb_access_token = (string) $accessToken; if (isset($accessToken)) { try { // Returns a `FacebookFacebookResponse` object $response = $this->Facebook->get('/me?fields=id,first_name,last_name,email',$accessToken); } catch (FacebookExceptionsFacebookResponseException $e) { $this->Session->setFlash('Graph returned an error: ' . $e->getMessage(),'error'); $this->redirect($this->referer()); } catch (FacebookExceptionsFacebookSDKException $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 } } } 解决方法
我在SDK和CakePHP 2.x上也遇到了一些问题.我写了一个小型处理程序,让SDK使用CakeSession.
你可以在这里找到它: https://github.com/WrDX/FacebookCakeSessionPersistentDataHandler (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |