Thursday 29 December 2011

Gotomeeting php api(oauth) implementation

I had a lot of problems connecting to citrix and making the api work, here are some steps to take:

Oauth integration:

$key = '#';
$secret = '#';

$domain = $_SERVER['HTTP_HOST'];
$base = "/oauth/index.php";
$base_url = urlencode("http://$domain$base");

$OAuth_url = "https://api.citrixonline.com/oauth/authorize?client_id=$key&redirect_uri=$base_url";
$OAuth_exchange_keys_url = "http://api.citrixonline.com/oauth/access_token?grant_type=authorization_code&code={responseKey}&client_id=$key";


if($_SESSION['access_token']) CreateForm();else
if($_GET['send']) OAuth_Authentication($OAuth_url);
elseif($_GET['code']) OAuth_Exchanging_Response_Key($_GET['code'],$OAuth_exchange_keys_url);

function createForm(){
// you are loged in, play around
}


function OAuth_Exchanging_Response_Key($code,$url){
 if($_SESSION['access_token']){
  CreateForm();
  return true;
 }
 $data = getURL(str_replace('{responseKey}',$code,$url));
 
 if(IsJsonString($data)){
  $data = json_decode($data);
  $_SESSION['access_token'] = $data->access_token;
  CreateForm();
 }else{
  echo 'error';
 }
}

function OAuth_Authentication ($url){
 $_SESSION['access_token'] = false;
 header("Location: $url");
}



/*
 * CURL function to get url
 */
function getURL($url,$auth_token = false,$data=false){

 // Initialize session and set URL.
 $ch = curl_init();
 
 curl_setopt($ch, CURLOPT_URL, $url);
        // curl not working local machine, using fiddler
 curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
 
 // Set so curl_exec returns the result instead of outputting it.
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
 if($auth_token){
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth oauth_token='.$auth_token));
 }
 
 // Get the response and close the channel.
 $response = curl_exec($ch);
 
 /*
  * if redirect, redirect
  */
 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
                     if ($code == 301 || $code == 302) { 
                         preg_match('//(.*?)">/', $response, $matches); 
                         $newurl = str_replace('&','&',trim(array_pop($matches))); 
       $response = getURL($newurl);
                     } else { 
                         $code = 0; 
                     }
 curl_close($ch);
 return $response;
}

To start login you need to access ?send=1. I am using fidler as a proxy for https requests because my server is not set up for this.

No comments:

Post a Comment