Advertisement
bandit84

gotomeeting oauth2 php

May 2nd, 2012
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.76 KB | None | 0 0
  1. Here's an example of how to get your oauth2.0 access token and get a list of your scheduled meetings.
  2. I adapted it from this example: http://stackoverflow.com/questions/9273576/gotowebinar-api-php. Please give that guy full credit.  I just changed a few lines of code.
  3.  
  4.  
  5. gotoMeetingClass.php:
  6. <?php
  7.  
  8. define('GOTO_MEETING_API_KEY','Your api key goes here');
  9.  
  10. class OAuth_En{
  11.  
  12. protected $_accessToken;
  13. protected $_userId;
  14. protected $_organizerKey;
  15. protected $_refreshToken;
  16. protected $_expiresIn;
  17.  
  18. public function getAccessToken(){
  19.     return $this->_accessToken;
  20. }
  21.  
  22. public function setAccessToken($token){
  23.     $this->_accessToken = $token;
  24. }
  25.  
  26. public function getUserId(){
  27.     return $this->_userId;
  28. }
  29.  
  30. public function setUserId($id){
  31.     $this->_userId = $id;
  32. }  
  33.  
  34. public function getOrganizerKey(){
  35.     return $this->_organizerKey;
  36. }
  37.  
  38. public function setOrganizerKey($key){
  39.     $this->_organizerKey = $key;
  40. }
  41.  
  42. public function getRefreshToken(){
  43.     return $this->_refreshToken;
  44. }
  45.  
  46. public function setRefreshToken($token){
  47.     $this->_refreshToken = $token;
  48. }
  49.  
  50. public function getExpiresIn(){
  51.     return $this->_expiresIn;
  52. }
  53.  
  54. public function setExpiresIn($expiresIn){
  55.     $this->_expiresIn = $expiresIn;
  56. }  
  57.  
  58.  
  59. }
  60.  
  61. class OAuth_Db{
  62. function getToken(){
  63.  
  64. }      
  65. }
  66.  
  67. class myOAuth{
  68. protected $_redirectUrl;
  69. protected $_OAuthEnObj;
  70. protected $_curlHeader = array();
  71. protected $_apiResponse;
  72. protected $_apiError;
  73. protected $_apiErrorCode;
  74. protected $_apiRequestUrl;
  75. protected $_apiResponseKey;
  76. protected $_accessTokenUrl;
  77. protected $_meetingId;
  78. protected $_registrantInfo = array();
  79. protected $_apiRequestType;
  80. protected $_apiPostData;
  81.  
  82. public function __construct(OAuth_En $oAuthEn){
  83.     $this->_OAuthEnObj = $oAuthEn;  
  84. }
  85.  
  86. public function getOAuthEntityClone(){
  87.     return clone $this->_OAuthEnObj;    
  88. }
  89.  
  90. public function getMeetingId(){
  91.     return $this->_meetingId;
  92. }
  93.  
  94. public function setMeetingId($id){
  95.     $id = (int)$id;
  96.     $this->_meetingId = empty($id) ? 0 : $id;
  97. }
  98.  
  99. public function setApiErrorCode($code){
  100.     $this->_apiErrorCode = $code;  
  101. }
  102.  
  103. public function getApiErrorCode(){
  104.     return $this->_apiErrorCode;    
  105. }  
  106.  
  107. public function getApiAuthorizationUrl(){
  108.     return 'https://api.citrixonline.com/oauth/authorize?client_id='.GOTO_MEETING_API_KEY.'&redirect_uri='.$this->getRedirectUrl();
  109. }
  110.  
  111. public function getApiKey(){
  112.     return  GOTO_MEETING_API_KEY;
  113. }
  114.  
  115. public function getApiRequestUrl(){
  116.     return  $this->_apiRequestUrl;
  117. }
  118.  
  119. public function setApiRequestUrl($url){
  120.     $this->_apiRequestUrl = $url;
  121. }
  122.  
  123. public function setRedirectUrl($url){
  124.     $this->_redirectUrl = urlencode($url);  
  125. }
  126.  
  127. public function getRedirectUrl(){
  128.     return $this->_redirectUrl;
  129. }
  130.  
  131. public function setCurlHeader($header){
  132.     $this->_curlHeader = $header;  
  133. }
  134.  
  135. public function getCurlHeader(){
  136.     return $this->_curlHeader;  
  137. }
  138.  
  139. public function setApiResponseKey($key){
  140.     $this->_apiResponseKey = $key;
  141. }
  142.  
  143. public function getApiResponseKey(){
  144.     return $this->_apiResponseKey;
  145. }
  146.  
  147. public function setRegistrantInfo($arrInfo){
  148.     $this->_registrantInfo = $arrInfo;  
  149. }
  150.  
  151. public function getRegistrantInfo(){
  152.     return $this->_registrantInfo;  
  153. }
  154.  
  155. public function authorizeUsingResponseKey($responseKey){
  156.     $this->setApiResponseKey($responseKey);
  157.     $this->setApiTokenUsingResponseKey();
  158. }
  159.  
  160. protected function setAccessTokenUrl(){
  161.     $url = 'https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code&code={responseKey}&client_id={api_key}';
  162.     $url = str_replace('{api_key}', $this->getApiKey(), $url);
  163.     $url = str_replace('{responseKey}', $this->getApiResponseKey(), $url);
  164.     $this->_accessTokenUrl = $url;
  165. }
  166.  
  167. protected function getAccessTokenUrl(){
  168.     return $this->_accessTokenUrl;  
  169. }
  170.  
  171. protected function resetApiError(){
  172.     $this->_apiError = '';  
  173. }
  174.  
  175. public function setApiTokenUsingResponseKey(){
  176.     //set the access token url
  177.     $this->setAccessTokenUrl();
  178.  
  179.     //set the url where api should go for request
  180.     $this->setApiRequestUrl($this->getAccessTokenUrl());
  181.  
  182.     //make request
  183.     $this->makeApiRequest();
  184.  
  185.     if($this->hasApiError()){
  186.         echo $this->getApiError();
  187.     }else{
  188.         //if api does not have any error set the token
  189.         echo $this->getResponseData();
  190.         $responseData = json_decode($this->getResponseData());
  191.         $this->_OAuthEnObj->setAccessToken($responseData->access_token);
  192.         $this->_OAuthEnObj->setOrganizerKey($responseData->organizer_key);
  193.         $this->_OAuthEnObj->setRefreshToken($responseData->refresh_token);
  194.         $this->_OAuthEnObj->setExpiresIn($responseData->expires_in);
  195.     }
  196. }
  197.  
  198. function hasApiError(){
  199.     return $this->getApiError() ? 1 : 0;
  200. }
  201.  
  202. function getApiError(){
  203.     return $this->_apiError;
  204. }
  205.  
  206. function setApiError($errors){
  207.     return $this->_apiError = $errors;
  208. }
  209.  
  210. function getApiRequestType(){
  211.     return $this->_apiRequestType;
  212. }
  213.  
  214. function setApiRequestType($type){
  215.     return $this->_apiRequestType = $type;
  216. }  
  217.  
  218. function getResponseData(){
  219.     return $this->_apiResponse;
  220. }
  221.  
  222. function setApiPostData($data){
  223.     return $this->_apiPostData = $data;
  224. }  
  225.  
  226. function getApiPostData(){
  227.     return $this->_apiPostData;
  228. }  
  229.  
  230. function makeApiRequest(){
  231.     $header = array();
  232.  
  233.     $this->getApiRequestUrl();
  234.     $ch = curl_init();
  235.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  236.     curl_setopt($ch, CURLOPT_URL, $this->getApiRequestUrl());
  237.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  238.  
  239.     if($this->getApiRequestType()=='POST'){
  240.         curl_setopt($ch, CURLOPT_POST, 1);
  241.         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->getApiPostData());  
  242.     }
  243.  
  244.     if($this->getCurlHeader()){
  245.         $headers = $this->getCurlHeader();
  246.     }else{
  247.         $headers = array(
  248.                 "HTTP/1.1",
  249.                 "Content-type: application/json",
  250.                 "Accept: application/json",
  251.                 "Authorization: OAuth oauth_token=".$this->_OAuthEnObj->getAccessToken()
  252.             );  
  253.     }
  254.  
  255.     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  256.  
  257.     $data = curl_exec($ch);
  258.     $validResponseCodes = array(200,201,409);
  259.     $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  260.  
  261.     $this->resetApiError();
  262.  
  263.     if (curl_errno($ch)) {
  264.         $this->setApiError(array(curl_error($ch)));
  265.     } elseif(!in_array($responseCode, $validResponseCodes)){
  266.         if($this->isJsonString($data)){
  267.             $data = json_decode($data);
  268.         }
  269.  
  270.         $this->setApiError($data);
  271.         $this->setApiErrorCode($responseCode);
  272.     }else {
  273.         $this->_apiResponse = $data;
  274.         $_SESSION['gotoApiResponse'] = $this->getResponseData();
  275.         curl_close($ch);
  276.     }
  277. }
  278.  
  279. function isAuthorizationRequiredAgain(){
  280.     $arrAuthorizationRequiredCodes = array(400,401,403,500);
  281.     $isAuthRequired = 0;
  282.     $error = $this->getApiError();
  283.     $responseCode = $this->getApiErrorCode();
  284.  
  285.     //we might have to add more exception in this condition
  286.     if(in_array($responseCode, $arrAuthorizationRequiredCodes)){
  287.         if($responseCode==400 && is_object($error)){    //because for 400 error sometime one needs to authenticate again
  288.             foreach($error as $single){
  289.                 $pos = strpos($single,'Authorization');
  290.                 if($pos!==false){
  291.                     $isAuthRequired = 1;
  292.                 }
  293.             }
  294.         }else{
  295.             $isAuthRequired = 1;    
  296.         }
  297.     }
  298.  
  299.     return $isAuthRequired;
  300. }
  301.  
  302. function getMeetings(){
  303.     $url = 'https://api.citrixonline.com/G2M/rest/meetings?scheduled=true&oauth_token='.$this->_OAuthEnObj->getAccessToken();        
  304.     $this->setApiRequestUrl($url);
  305.     $this->setApiRequestType('GET');
  306.     $this->makeApiRequest();
  307.  
  308.     if($this->hasApiError()){
  309.         return null;    
  310.     }
  311.  
  312.     $meetings = json_decode($this->getResponseData());
  313.     $test = $this->_OAuthEnObj->getAccessToken();
  314.     return $meetings;
  315. }
  316.  
  317. function getAuthURL(){
  318.     $url = 'https://api.citrixonline.com/G2M/rest/meetings?scheduled=true&oauth_token='.$this->_OAuthEnObj->getAccessToken();
  319.     return $url;
  320. }
  321.  
  322. function isJsonString($string){
  323.     $isJson = 0;
  324.     $decodedString = json_decode($string);
  325.     if(is_array($decodedString) || is_object($decodedString))
  326.         $isJson = 1;    
  327.  
  328.     return $isJson;
  329. }
  330. }
  331.  
  332. -----------------------------
  333. Authorize.php:
  334. <?php
  335. include_once "gotoMeetingClass.php";
  336. define('REDIRECT_URL_AFTER_AUTHENTICATION','http://example.com/authorize.php'); //this is the url where your get token code would be written.
  337.  
  338. session_start();
  339. $obj = new OAuth_En();
  340.  
  341. $oauth = new myOAuth($obj);
  342.  
  343.  
  344. if(!isset($_GET['code'])){  
  345.     goForAuthorization();
  346. }else{  //when user authenticates and redirect back to application redirect url, get the token
  347.     $oauth->authorizeUsingResponseKey($_GET['code']);
  348.     if(!$oauth->hasApiError()){
  349.         $objOAuthEn = $oauth->getOAuthEntityClone();
  350.         $_SESSION['oauthEn'] = serialize($objOAuthEn);
  351.         header('Location: get-all-meetings.php');      
  352.     }
  353. }
  354.  
  355. //this function has been used for getting the key using which we can get the access token
  356. function goForAuthorization(){
  357.     global $oauth;
  358.     $oauth->setRedirectUrl(REDIRECT_URL_AFTER_AUTHENTICATION);
  359.     $url = $oauth->getApiAuthorizationUrl();
  360.     header('Location: '.$url);
  361. }
  362.  
  363.  
  364.  
  365. --------------------
  366.  
  367. gotoMeetingClass.php:
  368. <?php
  369.  
  370. include_once "gotoMeetingClass.php";
  371. session_start();
  372. $obj = unserialize($_SESSION['oauthEn']);
  373.  
  374. /*
  375. this can be used to fetch the stored access token key and organizer key from database and use it without asking the authetication from user again
  376.  
  377. $obj = new OAuth_En();
  378. $obj->setAccessToken('token');
  379. $obj->setOrganizerKey('organizer key');
  380. */
  381.  
  382. $oauth = new myOAuth($obj);
  383. $meetings = $oauth->getMeetings();
  384.  
  385. echo '<pre>';
  386. if(!$oauth->hasApiError()){
  387.     print_r($meetings);
  388. }else{
  389.     print_r($oauth->getApiError());
  390.     print_r($oauth->getApiErrorCode());
  391.     print_r($oauth->getAuthURL());
  392. }
  393. exit;
  394.  
  395. /*$webinars = $oauth->getUpcomingWebinars();
  396.  
  397. if(!$oauth->hasApiError()){
  398.     print_r($webinars);
  399. }else{
  400.     print_r($oauth->getApiError());
  401. }
  402.  
  403. exit;
  404. $registrantInfo = array(
  405.     "firstName"=>"ashish",
  406.     "lastName"=>"mehta",
  407.     "email"=>"test@test.com",
  408. );
  409.  
  410. $oauth->setWebinarId(525120321);
  411. $oauth->setRegistrantInfo($registrantInfo);
  412.  
  413. $res = $oauth->createRegistrant();
  414. echo $oauth->getApiErrorCode();
  415. if(!$oauth->hasApiError()){
  416.     print_r($res);  
  417. }else{
  418.     echo 'error';
  419.     print_r($oauth->getApiError());
  420. }
  421.  
  422. exit;
  423. $oauth->setWebinarId(525120321);
  424. $webinar = $oauth->getWebinar();
  425.  
  426. if(!$oauth->hasApiError()){
  427.     print_r($webinar);  
  428.  
  429. }else{
  430.     print_r($oauth->getApiError());
  431.     echo $oauth->getApiErrorCode();
  432. }
  433. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement