Created
April 10, 2012 00:19
-
-
Save zacparker/2347564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This method give you a Login URL for Facebook Connect | |
* | |
* @param String callback_URL defaults to MOBILE_WEB_URL | |
* | |
* @return String return URL for facebook authorization dialog | |
*/ | |
public static function facebookConnectURL($callback_URL = MOBILE_WEB_URL) { | |
global $apikey; | |
if (self::isLoggedIn()) { | |
throw new ModelException('You already have a session, silly! You can\'t get any more logged in than this.', 'illegal_action'); | |
} | |
$authentication = array( | |
'api_key' => $apikey, | |
'sessionid' => null | |
); | |
$params = array($authentication, array($callback_URL)); | |
$api_request = array("method"=>'User.authenticateFromFacebook', "params"=>$params, "id"=>0); | |
$json = json_encode($api_request); | |
return 'https://graph.facebook.com/oauth/authorize?' | |
.'client_id='.FACEBOOK_APPID | |
.'&redirect_uri='.urlencode(SITE_URL.'/API/API.php?json_rpc='.urlencode($json)); | |
} | |
/** | |
* Authenticate from Facebook login callback | |
* | |
* This method is meant to be called by the Facebook Connect callback URL, | |
* and looks for the new request parameters appended by OAuth | |
* | |
* @param String callback_URL | |
* | |
* @return Redirects to callback URL with "fb_token" parameter appended containing access token | |
*/ | |
public static function authenticateFromFacebook($callback_URL = MOBILE_WEB_URL) { | |
global $apikey; | |
if (self::isLoggedIn()) { | |
throw new ModelException('You already have a session, silly! You can\'t get any more logged in than this.', 'illegal_action'); | |
} | |
if (array_key_exists('code', $_REQUEST)) { | |
$authentication = array( | |
'api_key' => $apikey, | |
'sessionid' => null | |
); | |
$params = array($authentication, array($callback_URL)); | |
$api_request = array("method"=>'User.authenticateFromFacebook', "params"=>$params, "id"=>0); | |
$json = json_encode($api_request); | |
$tokenfetchurl = 'https://graph.facebook.com/oauth/access_token?' | |
.'client_id='.FACEBOOK_APPID.'&' | |
.'client_secret='.FACEBOOK_SECRET.'&' | |
.'redirect_uri='.urlencode(SITE_URL.'/API/API.php?json_rpc='.urlencode($json)).'&' | |
.'code='.$_REQUEST['code']; | |
$tokendata = file_get_contents($tokenfetchurl); | |
if (empty($tokendata)) { | |
return false; | |
} | |
$pts = explode('&', $tokendata); | |
$args = array(); | |
foreach($pts as $pt) { | |
$arg = explode('=', $pt); | |
$args[$arg[0]] = $arg[1]; | |
} | |
$auth = self::authenticateWithSocialNetwork('Facebook', $args['access_token']); | |
if (!empty($auth) && array_key_exists('session', $auth)) { | |
$session_id = $auth['session']; | |
} | |
header('Location: ' . $callback_URL . '?fb_token=' . $args['access_token'] . '&session_id=' . $session_id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment