public function main() { try { $helper = new Facebook\FacebookRedirectLoginHelper(A()->signInUrl()); $session = $helper->getSessionFromRedirect(); if ($session) { new Session($session->getAccessToken()); } } catch (Facebook\FacebookAuthorizationException $e) { // This seems to be ok but log a warning to see if it happens. L()->warning($e); } R('/'); }
public function signin() { FacebookSession::setDefaultApplication(Ntentan::$config['social.facebook.app_id'], Ntentan::$config['social.facebook.secret']); $helper = new \Facebook\FacebookRedirectLoginHelper('http://paanoo.com/users/signin/facebook'); try { $session = $helper->getSessionFromRedirect(); if ($session === null) { header('Location: ' . $helper->getLoginUrl(array('email'))); } } catch (FacebookRequestException $ex) { } catch (\Exception $ex) { } if ($session) { try { $userRequest = new FacebookRequest($session, 'GET', '/me'); $user = $userRequest->execute()->getGraphObject(GraphUser::className())->asArray(); return array('firstname' => $user['first_name'], 'lastname' => $user['last_name'], 'key' => "facebook_{$user['id']}", 'avatar' => "http://graph.facebook.com/{$user['id']}/picture?type=large", 'email' => $user['email'], 'email_confirmed' => $user['verified'], 'avatar_format' => 'jpg'); } catch (Exception $ex) { } } }
/** * Get a Facebook access token * * @return bool|string */ function socialink_facebook_get_access_token() { if (!socialink_facebook_available()) { return false; } if (isset($_SESSION["socialink_facebook"]) && isset($_SESSION["socialink_facebook"]["callback"])) { $callback = $_SESSION["socialink_facebook"]["callback"]; $redirect = new Facebook\FacebookRedirectLoginHelper($callback); $session = $redirect->getSessionFromRedirect(); if (empty($session)) { return false; } unset($_SESSION["socialink_facebook"]); $token = $session->getAccessToken(); if (empty($token)) { return false; } $token->extend(); return (string) $token; } elseif (isset($_SESSION["socialink_token"])) { return $_SESSION["socialink_token"]; } return false; }
/** * アプリ許可URLを取得 * * @param $config * @return string */ public function getLoginUrl($config = array()) { if (isset($config['redirect_uri'])) { $url = $config['redirect_uri']; } else { $url = $this->getRequestUrl(); } $scope = array(); if (isset($config['scope'])) { if (is_array($config['scope'])) { $scope = $config['scope']; } else { if (is_string($config['scope'])) { $scope = explode(',', $config['scope']); } } } $helper = new Facebook\FacebookRedirectLoginHelper($url); $session = $helper->getSessionFromRedirect(); return $helper->getLoginUrl($scope); //return $this->facebook->getLoginUrl($config); }
<?php // start Session session_start(); // include composer autoloader include_once "libs/autoload.php"; // init facebook APP Facebook\FacebookSession::setDefaultApplication("app_id", "secret_key"); // create facebook app helper $helper = new Facebook\FacebookRedirectLoginHelper("http://localhost/fb_login/index.php"); try { if ($session = $helper->getSessionFromRedirect()) { $_SESSION['fb_token'] = $session->getToken(); header("Location: index.php"); } // check if facebook session isset // if true get user information if (isset($_SESSION['fb_token'])) { $session = new Facebook\FacebookSession($_SESSION['fb_token']); $request = new Facebook\FacebookRequest($session, "GET", "/me"); $request = $request->execute(); // get User Graph $user = $request->getGraphObject()->asArray(); //echo "<pre>", print_r($user), "</pre>"; } } catch (Facebook\FacebookRequestExeption $e) { // if facebook return an Error } catch (\Exeption $e) { // if local issue }
/** * Similar to onAuthenticate, except we already have a logged in user, we're just linking accounts * * @param array $options * @return void */ public function link($options = array()) { // Set up the config for the sdk instance $config = array('appId' => $this->params->get('app_id'), 'secret' => $this->params->get('app_secret')); // Set defaults \Facebook\FacebookSession::setDefaultApplication($config['appId'], $config['secret']); $helper = new \Facebook\FacebookRedirectLoginHelper(self::getReturnUrl($options['return'])); try { $session = $helper->getSessionFromRedirect(); } catch (\Facebook\FacebookRequestException $ex) { // When Facebook returns an error } catch (\Exception $ex) { // When validation fails or other local issues } // Make sure we have a user_id (facebook returns 0 for a non-logged in user) if (isset($user_id) && $user_id > 0 || isset($session) && $session) { try { $request = new \Facebook\FacebookRequest($session, 'GET', '/me'); $user_profile = $request->execute()->getGraphObject(\Facebook\GraphUser::className()); $id = $user_profile->getId(); $email = $user_profile->getProperty('email'); } catch (\Facebook\FacebookRequestException $e) { // Error message? $response->status = \Hubzero\Auth\Status::FAILURE; $response->error_message = Lang::txt('PLG_AUTHENTICATION_FACEBOOK_ERROR_RETRIEVING_PROFILE', $e->getMessage()); return; } $hzad = \Hubzero\Auth\Domain::getInstance('authentication', 'facebook', ''); // Create the link if (\Hubzero\Auth\Link::getInstance($hzad->id, $id)) { // This facebook account is already linked to another hub account App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_FACEBOOK_ACCOUNT_ALREADY_LINKED'), 'error'); } else { $hzal = \Hubzero\Auth\Link::find_or_create('authentication', 'facebook', null, $id); $hzal->user_id = User::get('id'); $hzal->email = $email; $hzal->update(); } } else { // User didn't authorize our app, or, clicked cancel App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_FACEBOOK_MUST_AUTHORIZE_TO_LINK', Config::get('sitename')), 'error'); } }
<?php session_start(); if (!isset($_SESSION['user'])) { require_once 'vendor/autoload.php'; Facebook\FacebookSession::setDefaultApplication('988836404521710', 'edab1116bb0541b7199e5a83b18ab679'); $facebook = new Facebook\FacebookRedirectLoginHelper("http://localhost/pxami/index.php"); try { if ($session = $facebook->getSessionFromRedirect()) { $_SESSION['user'] = $session->getToken(); header('Location: index.php'); } if (isset($_SESSION['user'])) { $session = new Facebook\FacebookSession($_SESSION['user']); $request = new Facebook\FacebookRequest($session, 'GET', '/me?fields=email, name,gender'); $request = $request->execute(); $user = $request->getGraphObject()->asArray(); $id = $user['id']; $name = $user['name']; $email = $user['email']; $gender = $user['gender']; include 'scripts/db.php'; //Check if already registered $query = "SELECT * FROM `user` WHERE `facebook_id` = {$id}"; $result = mysqli_query($link, $query); if ($result) { if (mysqli_num_rows($result) > 0) { $row = mysqli_fetch_array($result); $_SESSION['username'] = $row['username']; $_SESSION['email'] = $row['email']; $_SESSION['gender'] = $row['gender'];