1

嗨,我正在尝试通过 php curl 获取 Goto 会议 OAuth 访问令牌。但是当我打电话时它什么也不返回。请指导我如何获得它,代码如下。

$api_key = "123456"; $redirect_url = urlencode("URL");

$webinar_url = "https://api.citrixonline.com/oauth/authorize?client_id=".$api_key."&redirect_uri=".$redirect_url;

function getWebinarData($link) { $headers = array("HTTP/1.1", "Content-type: application/json", "Accept: application/json");

$curl = curl_init($link);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);      //2

$response = curl_exec($curl);
echo "<pre>DATA: ";print_r($response);echo "</pre>";
curl_close($curl);

return $response;

}

4

1 回答 1

1
/**
 * goto api 
 * Author: Ahad Ali
 * Date: valentines day 2013 
 * Classes Curl, OAuth, GotoTraining
 */
define ("API_KEY", "");
define ("REDIRECT_URL","");
define ("AUTH_AUTOLOGIN_URL","https://developer.citrixonline.com/oauth/g2t/authorize.php");
define ("AUTH_EXCHANGE_URL", "https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code&code=<CODE>&client_id=" . API_KEY);
define ("MANAGE_TRAINING_URL","https://api.citrixonline.com/G2T/rest/organizers/<ORGANIZERKEY>/trainings");

class Curl
{
    public $result;
    public function __construct()
    {

    }

    public function request($url, $data="", $method="get", $headers="")
    {
        $ch = curl_init();
        // this is autologiM USING CURL POST
        // avoiding the redirect to gotos site where it asks for email and password and redirects back to the URL with a code
        curl_setopt($ch, CURLOPT_URL, $url); 
        if($method == "post")
        {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if($headers)
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);          

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $this->result =  (string) curl_exec($ch);
        curl_close($ch);
        return $this->result;
    }

    public function __destruct()
    {

    }
}

class OAuth
{
    public $autologin_url;
    public $exchange_url;
    public $code;
    public $auth_result;
    //https://api.citrixonline.com/oauth/authorize?client_id= used this URL to get all the field names 
   public $login_data = array(
        'emailAddress' => '',
        'password' => '',
        'client_id' => '',
        'access_type'=> 'G2T',
        'app_name' => '',
        'redirect_uri' => '',
        'submitted' => 'form_submitted',
        );

   public function __construct($autologin_url = AUTH_AUTOLOGIN_URL, $exchange_url = AUTH_EXCHANGE_URL, $apikey=API_KEY)
   {
       $this->autologin_url = $autologin_url;
       $this->exchange_url = $exchange_url;
       $this->login_data['client_id'] = $apikey;
   }
   public function authorize()
   {
       $this->getCode();
       $this->exchangeCodeForAccessToken();

   }
   public function getCode()
   {
       $curl = new Curl();
       $result = $curl->request($this->autologin_url, $this->login_data, "post");
       $arr = explode("\n", $result);
        foreach($arr as $k=>$v)
        {
                if(strstr($v,"Location: http:"))
                        $return_url = $v;
        }
        $query = trim(parse_url($return_url, PHP_URL_QUERY));// adds one unnecessary _ (underscore) at the end of the query string
        $this->code = substr($query, 5, (strlen($query) - 6));//starting from 5 get me ...number of chars
   }
   function exchangeCodeForAccessToken()
   {
       $this->exchange_url = str_replace("<CODE>", $this->code, $this->exchange_url);
       $curl = new Curl();
       $result = $curl->request($this->exchange_url);
       $this->auth_result = json_decode($result);
    }
   public function __destruct()
   {

   }
}

class GotoTraining extends OAuth
{
    public $manage_training_url;
    public $training_result;
    public $error_list = array("AuthFailure", "AccessDenied", "ExpiredToken", "InternalError", "InvalidRequest", "InvalidMethod", "MissingToken", "NoSuchTraining", "InvalidToken");
    public function __construct($url = MANAGE_TRAINING_URL)
    {
        $this->manage_training_url = $url;
        parent::__construct();
    }
    /**
 *Arguement List for goto CreateTraining service
 *  [name] => Representational State Transfer 101
    [description] => The REST-ful way to APIs.
    [timeZone] => America/Los_Angeles
    [times] => Array
        (
            [0] => stdClass Object
                (
                    [startDate] => 2011-09-08T18:25:00Z
                    [endDate] => 2011-09-08T19:25:00Z
                )

            [1] => stdClass Object
                (
                    [startDate] => 2011-09-09T18:25:00Z
                    [endDate] => 2011-09-09T19:25:00Z
                )

        )

    [registrationSettings] => stdClass Object
        (
            [disableWebRegistration] => false
            [disableConfirmationEmail] => false
        )

    [organizers] => Array
        (
            [0] => 6512477
            [1] => 38712
            [2] => 9876466
        ) 
 */
    public function createTraining($name, $desc, $times)
    {

        $registrationSettings["disableWebRegistration"] = "false";
        $registrationSettings["disableConfirmationEmail"] = "false";

        $json["name"] = $name;
        $json["description"] = $desc;
        $json["timeZone"] = "Australia/Sydney";
        $json["times"] = $times;//array for startDate, endDate
        $json["registrationSettings"] = $registrationSettings;
        $json["organizers"][0] =  $this->auth_result->organizer_key;

        $this->manage_training_url = str_replace("<ORGANIZERKEY>", $this->auth_result->organizer_key, $this->manage_training_url);
        $json = json_encode($json);
        //$post_data[] = "Authorization:OAuth oauth_token=" . $this->auth_result->access_token;
        //$this->manage_training_url = $this->manage_training_url . "?oauth_token=" . $this->auth_result->access_token;

        $headers =  array(
               'Accept: application/json',
              'Content-Type: application/json', 
              'Authorization: OAuth oauth_token=' . $this->auth_result->access_token
              );

        //$this->manage_training_url = $this->manage_training_url . "?oauth_token=" . $this->auth_result->access_token;
        $curl = new Curl();
        $this->training_result = $curl->request($this->manage_training_url, $json, "post", $headers);
        $arr = explode("\n", $this->training_result);
        $this->webCode = trim($arr[count($arr)-1], '"');

        $this->checkError();

        return $this->webCode;
    }

    public function checkError()
    {
        foreach($this->error_list as $val)
        {
            if(strstr($this->training_result, $val))
                $this->webCode = $val;
        }
        return 0;
    }
}
于 2013-02-13T05:20:40.500 回答