isleshocky77

Using 2-legged OAuth 1.0 with Google Calendar API

Mar 14th, 2012
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.43 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Depends on oauth-php library
  4.  * @link http://code.google.com/p/oauth-php/
  5.  * svn co http://oauth-php.googlecode.com/svn/trunk/library/
  6.  */
  7.  
  8. abstract class sfGoogleApi {
  9.  
  10.   protected $developerKey;
  11.  
  12.   protected $consumerKey;
  13.  
  14.   protected $consumerSecret;
  15.  
  16.   protected $requestorId;
  17.  
  18.   protected $consumer;
  19.  
  20.   protected $parameters;
  21.  
  22.   public function __construct($requestId) {
  23.  
  24.     $this->requestorId = $requestId;
  25.  
  26.     # Setup Consumer
  27.    $this->developerKey   = sfConfig::get('app_sf_google_api_plugin_developer_key');
  28.     $this->consumerKey    = sfConfig::get('app_sf_google_api_plugin_consumer_key');
  29.     $this->consumerSecret = sfConfig::get('app_sf_google_api_plugin_consumer_secret');
  30.  
  31.     $this->consumer = new OAuthConsumer($this->consumerKey, $this->consumerSecret, NULL);
  32.     $this->parameters = array(
  33.       'xoauth_requestor_id' => $this->requestorId,
  34.       'key' => $this->developerKey,
  35.     );
  36.   }
  37.  
  38.   public function getRequestorId() {
  39.  
  40.     return $this->requestorId;
  41.   }
  42.  
  43.  
  44.   /**
  45.    * Makes an HTTP request to the specified URL
  46.    * @param string $http_method The HTTP method (GET, POST, PUT, DELETE)
  47.    * @param string $url Full URL of the resource to access
  48.    * @param string $auth_header (optional) Authorization header
  49.    * @param string $postData (optional) POST/PUT request body
  50.    * @return string Response body from the server
  51.    */
  52.   protected function send_request($http_method, $url, $auth_header=null, $postData=null, $extra_headers = array()) {
  53.     $curl = curl_init($url);
  54.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  55.     curl_setopt($curl, CURLOPT_FAILONERROR, false);
  56.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  57.     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  58.  
  59.     $headers = array();
  60.  
  61.     if($auth_header) {
  62.       $headers = array_merge($headers, array($auth_header));
  63.     }
  64.  
  65.     if($extra_headers) {
  66.       $headers = array_merge($headers, $extra_headers);
  67.     }
  68.  
  69.     switch($http_method) {
  70.       case 'GET':
  71.         break;
  72.       case 'POST':
  73.         if(!isset($extra_headers['content-type'])) {
  74.           $headers[] = 'Content-Type: application/json; charset=UTF-8';
  75.         } else {
  76.           $headers[] = $extra_headers['content-type'];
  77.         }
  78.  
  79.         curl_setopt($curl, CURLOPT_POST, 1);
  80.         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  81.         break;
  82.       case 'PUT':
  83.         $headers[] = 'Content-Type: application/atom-xml';
  84.         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
  85.         curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  86.         break;
  87.       case 'DELETE':
  88.         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
  89.         break;
  90.     }
  91.  
  92.     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  93.     curl_setopt($curl, CURLOPT_VERBOSE, true);
  94.  
  95.     $response = curl_exec($curl);
  96.     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  97.     if (!$response) {
  98.       $response = curl_error($curl);
  99.     }
  100.  
  101.     curl_close($curl);
  102.     return $response;
  103.   }
  104.  
  105.   /**
  106.    * Joins key:value pairs by inner_glue and each pair together by outer_glue
  107.    * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE)
  108.    * @param string $outer_glue Full URL of the resource to access
  109.    * @param array $array Associative array of query parameters
  110.    * @return string Urlencoded string of query parameters
  111.    */
  112.   protected function implode_assoc($inner_glue, $outer_glue, $array) {
  113.     $output = array();
  114.     foreach($array as $key => $item) {
  115.       $output[] = $key . $inner_glue . urlencode($item);
  116.     }
  117.     return implode($outer_glue, $output);
  118.   }
  119. }
  120.  
  121. class sfGoogleApiCalendar extends sfGoogleApi {
  122.  
  123.   /**
  124.    * Creates a new calendar in Google
  125.    * @param string $title
  126.    * @param string $summary
  127.    * @param boolean $selected
  128.    * @return Zend_Gdata_Calendar_ListEntry
  129.    */
  130.   public function createCalendar($summary, $description, $colorId = 1, $selected = true) {
  131.  
  132.     $base_feed = 'https://www.googleapis.com/calendar/v3/calendars';
  133.  
  134.     $request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'POST', $base_feed, $this->parameters);
  135.     $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, null);
  136.  
  137.     $data = json_encode(array(
  138.       'summary'     => $summary,
  139.       'description' => $description,
  140.       'colorId'     => $colorId,
  141.       'selected'    => true
  142.     ));
  143.  
  144.     // Make signed OAuth request to the Contacts API server
  145.     $url = $base_feed . '?' . $this->implode_assoc('=', '&', $this->parameters);
  146.     $response = $this->send_request($request->get_normalized_http_method(), $url, $request->to_header(), $data);
  147.  
  148.     $object = json_decode($response);
  149.  
  150.     if(isset($object->error)) {
  151.       return false;
  152.     }
  153.  
  154.     return $object;
  155.   }
  156.  
  157.   /**
  158.    * Returns all calendars for a user
  159.    * @return array[stdClass] An array of all the Calendars
  160.    */
  161.   public function getCalendars() {
  162.  
  163.     $base_feed = 'https://www.googleapis.com/calendar/v3/users/me/calendarList';
  164.     $request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'GET', $base_feed, $this->parameters);
  165.     $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, null);
  166.  
  167.     // Make signed OAuth request to the Contacts API server
  168.     $url = $base_feed . '?' . $this->implode_assoc('=', '&', $this->parameters);
  169.  
  170.     $response = $this->send_request($request->get_normalized_http_method(), $url, $request->to_header());
  171.  
  172.     $object = json_decode($response);
  173.  
  174.     if(isset($object->error)) {
  175.       return false;
  176.     }
  177.  
  178.     return $object->items;
  179.   }
  180.  
  181.   /**
  182.    * Gets a specific calendar.
  183.    * @param string $calendarId
  184.    * @return stdClass|false The Calendar object
  185.    */
  186.   public function getCalendar($calendarId) {
  187.  
  188.     $base_feed = 'https://www.googleapis.com/calendar/v3/users/me/calendarList/';
  189.  
  190.     # Get specific calendar
  191.    $base_feed .= $calendarId;
  192.  
  193.     $request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'GET', $base_feed, $this->parameters);
  194.     $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, null);
  195.  
  196.     // Make signed OAuth request to the Contacts API server
  197.     $url = $base_feed . '?' . $this->implode_assoc('=', '&', $this->parameters);
  198.  
  199.     $response = $this->send_request($request->get_normalized_http_method(), $url, $request->to_header());
  200.  
  201.     $object = json_decode($response);
  202.  
  203.     if(isset($object->error)) {
  204.       return false;
  205.     }
  206.  
  207.     return $object;
  208.   }
  209.  
  210.   /**
  211.    * Inserts a new event into the Calendar
  212.    * @param stdClass $event
  213.    * @param string $calendarId
  214.    */
  215.   public function insertEvent($event, $calendarId = null) {
  216.  
  217.     $base_feed = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';
  218.  
  219.     if($calendarId) {
  220.       $base_feed = str_replace('primary', $calendarId, $base_feed);
  221.     }
  222.  
  223.     $request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'POST', $base_feed, $this->parameters);
  224.     $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, null);
  225.  
  226.     $data = json_encode($event);
  227.  
  228.     // Make signed OAuth request to the Contacts API server
  229.     $url = $base_feed . '?' . $this->implode_assoc('=', '&', $this->parameters);
  230.     $response = $this->send_request($request->get_normalized_http_method(), $url, $request->to_header(), $data);
  231.  
  232.     $object = json_decode($response);
  233.  
  234.     if(isset($object->error)) {
  235.       return false;
  236.     }
  237.  
  238.     return $object;
  239.   }
  240.  
  241.   /**
  242.    * Executes a batch request to the Google Calendar v2.1
  243.    * @param string $calendarId
  244.    * @param string $batch XML Batch request
  245.    */
  246.   public function executeBatch($calendarId, $batch) {
  247.     $base_feed = 'https://www.google.com/calendar/feeds/'.$calendarId.'/private/full/batch';
  248.  
  249.     $request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'POST', $base_feed, $this->parameters);
  250.     $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, null);
  251.  
  252.     // Make signed OAuth request to the Contacts API server
  253.     $url = $base_feed . '?' . $this->implode_assoc('=', '&', $this->parameters);
  254.     $response = $this->send_request($request->get_normalized_http_method(), $url, $request->to_header(), $batch,
  255.       array('Gdata-Version: 2.1'));
  256.   }
  257. }
  258.  
  259. /**
  260.  * Example Use
  261.  */
  262.  
  263. $sfGoogleCalendar = new sfGoogleCalendar($UserProgram->sfGuardUser->getGoogleUsername());
  264. $calendar = $sfGoogleCalendar->createCalendar($calendarTitle, $calendarSummary);
Add Comment
Please, Sign In to add comment