public function getCallback() { try { $session = $this->helper->getSessionFromRedirect(); } catch (FacebookRequestException $ex) { // When Facebook returns an error } catch (\Exception $ex) { // When validation fails or other local issues } if ($session) { $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $fbuser = $response->getGraphObject(); if ($fbuser) { return Redirect::to('/facebook/checkuser')->with('sessionfb', $session)->with('userfb', $fbuser)->withCookie(Cookie::forever('user', $fbuser)); } else { dd('Error'); } } }
<?php include 'header2.php'; include './1353/fbconfig.php'; //session_start(); $request = new FacebookRequest($session, 'GET', '/{album-id}/photos'); $response = $request->execute(); $graphObject = $response->getGraphObject(); $image_name = $graphObject->getProperty('images'); $_SESSION['IMAGE'] = $image_name; include 'footer.php';
public function fb_login() { echo "sdxcxcfsdf"; echo "i am here"; $this->layout = 'ajax'; FacebookSession::setDefaultApplication('195891030745689', '130d8f7bcae67a9a71d2c1c372896814'); $helper = new FacebookRedirectLoginHelper(FACEBOOK_REDIRECT_URI); echo "======="; $session = $helper->getSessionFromRedirect(); if (isset($_SESSION['token'])) { pr($_SESSION); echo "<<<<<<>>>>>>"; $session = new FacebookSession($_SESSION['token']); try { echo "XXXXXXXXXX"; $session->validate(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET); } catch (FacebookAuthorizationException $e) { echo $e->getMessage(); } } $data = array(); $fb_data = array(); if (isset($session)) { $_SESSION['token'] = $session->getToken(); $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); $graph = $response->getGraphObject(GraphUser::className()); $fb_data = $graph->asArray(); $id = $graph->getId(); $image = "https://graph.facebook.com/" . $id . "/picture?width=100"; if (!empty($fb_data)) { $result = $this->User->findByEmail($fb_data['email']); if (!empty($result)) { if ($this->Auth->login($result['User'])) { $this->Session->setFlash(FACEBOOK_LOGIN_SUCCESS, 'default', array('class' => 'message success'), 'success'); //$this->redirect(BASE_PATH); } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'login'); } } else { $data['email'] = $fb_data['email']; $data['first_name'] = $fb_data['first_name']; $data['social_id'] = $fb_data['id']; $data['picture'] = $image; $data['uuid'] = String::uuid(); $this->User->save($data); if ($this->User->save($data)) { $data['id'] = $this->User->getLastInsertID(); if ($this->Auth->login($data)) { $this->Session->setFlash(FACEBOOK_LOGIN_SUCCESS, 'default', array('class' => 'message success'), 'success'); //$this->redirect(BASE_PATH); } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } } else { $this->Session->setFlash(FACEBOOK_LOGIN_FAILURE, 'default', array('class' => 'message error'), 'error'); //$this->redirect(BASE_PATH.'index'); } } }
/** * This hook allows other modules to act on user creation via Facebook login. * * There is also a Rules event triggered that can be used to react * on user creation via Simple FB Connect. * * The example code shows how to make an additional query to Facebook API in * order to request user's first and last name and how to map these to * corresponding Drupal user fields. * * This example assumes that you have added fields "field_first_name" and * "field_last_name" to User entity at admin/config/people/accounts/fields and * that the length of these text fields is 255 characters. * * List of User fields on Facebook: * https://developers.facebook.com/docs/graph-api/reference/user * * @param $drupal_user * Drupal user that was just created via Simple FB Connect. */ function hook_simple_fb_connect_registration($drupal_user) { // Implement this hook in your own module to act on user creation. // The code here is just an example. // Get FacebookSession for current user. $fb_session = simple_fb_connect_get_session(); // Try to read first and last name from Facebook API. try { $request = new FacebookRequest($fb_session, 'GET', '/me?fields=first_name,last_name'); $object = $request->execute()->getGraphObject(); // Truncate Facebook values to 255 characters. $first_name = substr($object->getProperty('first_name'), 0, 255); $last_name = substr($object->getProperty('last_name'), 0, 255); // Save Facebook valuest to Drupal user $drupal_user->field_first_name[LANGUAGE_NONE][0]['value'] = $first_name; $drupal_user->field_last_name[LANGUAGE_NONE][0]['value'] = $last_name; // Save the user. user_save($drupal_user); } catch (FacebookRequestException $ex) { watchdog('YOURMODULE', 'Could not load fields from Facebook: FacebookRequestException. Error details: @message', array('@message' => json_encode($ex->getResponse())), WATCHDOG_ERROR); } catch (\Exception $ex) { watchdog('YOURMODULE', 'Could not load fields from Facebook: Unhandled exception. Error details: @message', array('@message' => $ex->getMessage()), WATCHDOG_ERROR); } }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { $this->loadState(); if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $request = new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params); $response = $request->execute()->getResponse(); if (isset($response['access_token'])) { return new FacebookSession($response['access_token']); } } return null; }
/** * newSessionAfterValidation - Returns a FacebookSession for a * validated & parsed signed request. * * @param array $parsedSignedRequest * * @return FacebookSession * * @throws FacebookRequestException */ private static function newSessionAfterValidation($parsedSignedRequest) { $params = array('client_id' => self::$defaultAppId, 'redirect_uri' => '', 'client_secret' => self::$defaultAppSecret, 'code' => $parsedSignedRequest['code']); $fbRequest = new FacebookRequest(self::newAppSession(self::$defaultAppId, self::$defaultAppSecret), 'GET', '/oauth/access_token', $params); $response = $fbRequest->execute()->getResponse(); if (isset($response['access_token'])) { return new FacebookSession($response['access_token']); } throw FacebookRequestException::create(json_encode($parsedSignedRequest), $parsedSignedRequest, 401); }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { $this->loadState(); if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $fbRequest = new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params); $response = $fbRequest->execute()->getResponse(); // echo __METHOD__ . __LINE__ . " Facebook Authentication response <br><pre>";var_dump($response);echo "</pre><br>"; if (isset($response['access_token'])) { return new FacebookSession($response['access_token']); } } return null; }
protected function action() { //Init app with app id and secret FacebookSession::setDefaultApplication('416201525206070', 'db946f783550299dcf77faf9f09a8f41'); //Login helper with redirect_uri $helper = new FacebookRedirectLoginHelper('http://www.ituk.it/cinderella/fblogin'); try { $session = $helper->getSessionFromRedirect(); } catch (FacebookRequestException $ex) { //Facebook returns an error Session::setObj(Session::SYSMSG, 'Errore durante l\'autenticazione con Facebook, per favore riprovare più tardi.'); $this->redirect = 'message'; exit; } catch (Exception $ex) { //Validation fails or other local issues Session::setObj(Session::SYSMSG, 'Errore durante l\'autenticazione con Facebook, per favore riprovare più tardi.'); $this->redirect = 'message'; exit; } //See if we have a session if (isset($session)) { //Graph api request for user data $request = new FacebookRequest($session, 'GET', '/me'); $response = $request->execute(); //Get response $graphObject = $response->getGraphObject(); $fbid = $graphObject->getProperty('id'); // To Get Facebook ID $fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name $fbfirstname = $graphObject->getProperty('first_name'); // To Get Facebook first name $fblastname = $graphObject->getProperty('last_name'); // To Get Facebook last name $fbemail = $graphObject->getProperty('email'); // To Get Facebook email ID //Check if already logged in with Facebook //Create new Facebook user $fb_user = new UtenteFb($fbid); if (!$fb_user->hereIam()) { //Save credentials as regular user $objDateTime = new DateTime('NOW'); $fakepassw = md5($objDateTime->format('c')); $fakeemail = $fakepassw . '@facebook.com'; $fb_user->setNome($fbfirstname); $fb_user->setCognome($fblastname); $fb_user->setEmail(isset($fbemail) && strlen($fbemail) > 0 ? $fbemail : $fakeemail); $fb_user->setUsername('FacebookUser'); $fb_user->set_password($fakepassw); $fb_user->save(); //Save as Facebook user $fb_user->setFbId($fbid); $fb_user->setAvatarUrl($fb_user->getAvatarUrl()); //Save Facebook credentials $fb_user->saveAsFbUser(); } //Login user Session::setObj(Session::UTENTE, $fb_user); Session::setObj(Session::SYSMSG, 'Benvenuto ' . $fb_user->getNome() . ' ' . $fb_user->getCognome()); $this->redirect = 'message'; } else { //Try Facebook Authentication $loginUrl = $helper->getLoginUrl(); $this->redirect = $loginUrl; } }
/** * Handles a response from Facebook, including a CSRF check, and returns a * FacebookSession. * * @return FacebookSession|null */ public function getSessionFromRedirect() { if ($this->isValidRedirect()) { $params = array('client_id' => FacebookSession::_getTargetAppId($this->appId), 'redirect_uri' => $this->redirectUrl, 'client_secret' => FacebookSession::_getTargetAppSecret($this->appSecret), 'code' => $this->getCode()); $request = new FacebookRequest(FacebookSession::newAppSession($this->appId, $this->appSecret), 'GET', '/oauth/access_token', $params); $response = $request->execute(); $response = $response->getResponse(); // Graph v2.3 and greater return objects on the /oauth/access_token endpoint $accessToken = null; if (is_object($response) && isset($response->access_token)) { $accessToken = $response->access_token; } elseif (is_array($response) && isset($response['access_token'])) { $accessToken = $response['access_token']; } if (isset($accessToken)) { return new FacebookSession($accessToken); } } return null; }