/**
  * Authorize the user against the api_key, app_id, or canvas_url
  *
  * This produces a SocialSession Object.
  *
  * Possible Params:
  * network_key
  * trust_key
  * api_key
  * canvas_url
  * auth_token
  * social_callback
  */
 public function authorize()
 {
     $network_session = null;
     $network_key = $this->getParam('network_key');
     $auth_token = $this->getParam('auth_token');
     $social_callback = $this->getParam('social_callback');
     $api_key = $this->getParam('api_key');
     $canvas_url = $this->getParam('canvas_url');
     $user_name = $this->getParam('user_name');
     $trust_key = $this->getParam('trust_key');
     if (!isset($trust_key)) {
         $trust_key = $socialApiKey;
     }
     $result = $this->getAppProperties();
     if ($result) {
         $callback = isset($result['callback_url']) ? $result['callback_url'] : '';
         $apiKey = isset($result['api_key']) ? $result['api_key'] : '';
         $apiSecret = isset($result['secret_key']) ? $result['secret_key'] : '';
         if (!isset($social_callback)) {
             $social_callback = $callback;
         }
         try {
             if (isset($apiKey) && isset($apiSecret)) {
                 $auth_url = $this->getAuthUrl($trust_key);
                 $fb = new RingsideApiClients($apiKey, $apiSecret, null, $auth_url);
                 //public function __construct($api_key, $secret, $session_key = null, $url = null) {
                 $result = $fb->do_get_session($auth_token);
                 $session_key = $fb->api_client->session_key;
                 $uid = $fb->api_client->users_getLoggedInUser();
                 $pids = $fb->api_client->users_mapToSubject(array($uid), $network_key, $result['application_id']);
                 //					RingsideSocialDbPrincipal::getPrincipalForSubject($uid, $network_key, $user_name, $trust_key);
                 //if ( isset($pids) ) {
                 // getPrincipalForSubject accepts and returns multiple IDs
                 $pid = 0;
                 if (isset($pids)) {
                     $pid = $pids[0];
                 }
                 // bool setcookie ( string $name [, string $value [, int $expire [, string $path [, string $domain [, bool $secure [, bool $httponly ]]]]]] )
                 $network_session = new RingsideSocialSession();
                 $network_session->setNetwork($network_key);
                 $network_session->addApiSessionKey($apiKey, $session_key);
                 $network_session->setUserId($uid);
                 $network_session->setPrincipalId($pid);
                 $network_session->setTrust($trust_key);
                 $network_session->setCallbackUrl($social_callback);
                 $network_session->setLoggedIn(true);
                 $context = $this->getContext($fb->api_client, $network_session);
                 if (strrpos($social_callback, '?') == 0) {
                     return $social_callback . '?' . $context;
                 } else {
                     return $social_callback . '&' . $context;
                 }
                 //} else {
                 //	$this->error = "Unable to set Principle!";
                 //}
             }
         } catch (Exception $exception) {
             error_log("Exception : " . $exception->getMessage() . "\n" . $exception->getTraceAsString());
             $this->error = "Exception : " . $exception->getMessage() . "\n" . $exception->getTraceAsString();
         }
     }
     if (!isset($network_session)) {
         error_log("Application with api_key: {$api_key} or canvas_url: {$canvas_url} not found!  Creating session and redirecting to {$social_callback}!");
         $network_session = new RingsideSocialSession(null);
         $network_session->setNetwork($network_key);
         $network_session->setTrust($trust_key);
         $network_session->setCallbackUrl($social_callback);
         if (strrpos($social_callback, '?') == 0) {
             return $social_callback . "?social_session_key=" . $network_session->getSessionKey();
         } else {
             return $social_callback . "?social_session_key=" . $network_session->getSessionKey();
         }
     }
 }