diff options
Diffstat (limited to 'zend/demos/Zend/Gdata/3LeggedOAuth')
| -rwxr-xr-x | zend/demos/Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php | 109 | ||||
| -rwxr-xr-x | zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png | bin | 0 -> 5294 bytes | |||
| -rwxr-xr-x | zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png | bin | 0 -> 5696 bytes | |||
| -rwxr-xr-x | zend/demos/Zend/Gdata/3LeggedOAuth/index.php | 190 | ||||
| -rwxr-xr-x | zend/demos/Zend/Gdata/3LeggedOAuth/style.css | 43 |
5 files changed, 342 insertions, 0 deletions
diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php b/zend/demos/Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php new file mode 100755 index 0000000..f0edd57 --- /dev/null +++ b/zend/demos/Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php @@ -0,0 +1,109 @@ +<?php +require_once 'Zend/Oauth/Consumer.php'; +require_once 'Zend/Gdata/Query.php'; + +/** + * Wrapper class for Google's OAuth implementation. In particular, this helper + * bundles the token endpoints and manages the Google-specific parameters such + * as the hd and scope parameter. + * + * @category Zend + * @package Zend_Gdata + * @subpackage Demos + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +class Gdata_OAuth_Helper extends Zend_Oauth_Consumer { + // Google's default oauth parameters/constants. + private $_defaultOptions = array( + 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, + 'version' => '1.0', + 'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken', + 'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken', + 'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken' + ); + + /** + * Create Gdata_OAuth_Helper object + * + * @param string $consumerKey OAuth consumer key (domain). + * @param string $consumerSecret (optional) OAuth consumer secret. Required if + * using HMAC-SHA1 for a signature method. + * @param string $sigMethod (optional) The oauth_signature method to use. + * Defaults to HMAC-SHA1. RSA-SHA1 is also supported. + */ + public function __construct($consumerKey, $consumerSecret=null, + $sigMethod='HMAC-SHA1') { + $this->_defaultOptions['consumerKey'] = $consumerKey; + $this->_defaultOptions['consumerSecret'] = $consumerSecret; + $this->_defaultOptions['signatureMethod'] = $sigMethod; + parent::__construct($this->_defaultOptions); + } + + /** + * Getter for the oauth options array. + * + * @return array + */ + public function getOauthOptions() { + return $this->_defaultOptions; + } + + /** + * Fetches a request token. + * + * @param string $scope The API scope or scopes separated by spaces to + * restrict data access to. + * @param mixed $callback The URL to redirect the user to after they have + * granted access on the approval page. Either a string or + * Zend_Gdata_Query object. + * @return Zend_OAuth_Token_Request|null + */ + public function fetchRequestToken($scope, $callback) { + if ($callback instanceof Zend_Gdata_Query) { + $uri = $callback->getQueryUrl(); + } else { + $uri = $callback; + } + + $this->_defaultOptions['callbackUrl'] = $uri; + $this->_config->setCallbackUrl($uri); + if (!isset($_SESSION['ACCESS_TOKEN'])) { + return parent::getRequestToken(array('scope' => $scope)); + } + return null; + } + + /** + * Redirects the user to the approval page + * + * @param string $domain (optional) The Google Apps domain to logged users in + * under or 'default' for Google Accounts. Leaving this parameter off + * will give users the universal login to choose an account to login + * under. + * @return void + */ + public function authorizeRequestToken($domain=null) { + $params = array(); + if ($domain != null) { + $params = array('hd' => $domain); + } + $this->redirect($params); + } + + /** + * Upgrades an authorized request token to an access token. + * + * @return Zend_OAuth_Token_Access||null + */ + public function fetchAccessToken() { + if (!isset($_SESSION['ACCESS_TOKEN'])) { + if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) { + return parent::getAccessToken( + $_GET, unserialize($_SESSION['REQUEST_TOKEN'])); + } + } + return null; + } +} diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png b/zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png Binary files differnew file mode 100755 index 0000000..e77c523 --- /dev/null +++ b/zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png b/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png Binary files differnew file mode 100755 index 0000000..39de3a1 --- /dev/null +++ b/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/index.php b/zend/demos/Zend/Gdata/3LeggedOAuth/index.php new file mode 100755 index 0000000..59aab08 --- /dev/null +++ b/zend/demos/Zend/Gdata/3LeggedOAuth/index.php @@ -0,0 +1,190 @@ +<?php +/** + * Zend Framework + * + * LICENSE + * + * This source file is subject to the new BSD license that is bundled + * with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://framework.zend.com/license/new-bsd + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@zend.com so we can send you a copy immediately. + * + * @category Zend + * @package Zend_Gdata + * @subpackage Demos + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +/** + * Sample code to demonstrate accessing a Google Data feed using OAuth for + * authorization. Utilizes the Zend Framework Zend_OAuth components to + * communicate with the API(s). + * + * NOTE: As this is sample code, not all of the functions do full error + * handling. + */ + +require_once 'Zend/Gdata/Docs.php'; +require_once 'Zend/Gdata/Spreadsheets.php'; + +require_once 'Gdata_OAuth_Helper.php'; + +session_start(); + +// Application constants. Replace these values with your own. +$APP_NAME = 'google-ZendGData3LOSample-1.0'; +$APP_URL = getAppURL(); +$scopes = array( + 'https://docs.google.com/feeds/', + 'http://spreadsheets.google.com/feeds/' +); + +// Setup OAuth consumer. Thes values should be replaced with your registered +// app's consumer key/secret. +$CONSUMER_KEY = 'anonymous'; +$CONSUMER_SECRET = 'anonymous'; +$consumer = new Gdata_OAuth_Helper($CONSUMER_KEY, $CONSUMER_SECRET); + +// Main controller logic. +switch (@$_REQUEST['action']) { + case 'logout': + logout($APP_URL); + break; + case 'request_token': + $_SESSION['REQUEST_TOKEN'] = serialize($consumer->fetchRequestToken( + implode(' ', $scopes), $APP_URL . '?action=access_token')); + $consumer->authorizeRequestToken(); + break; + case 'access_token': + $_SESSION['ACCESS_TOKEN'] = serialize($consumer->fetchAccessToken()); + header('Location: ' . $APP_URL); + break; + default: + if (isset($_SESSION['ACCESS_TOKEN'])) { + $accessToken = unserialize($_SESSION['ACCESS_TOKEN']); + + $httpClient = $accessToken->getHttpClient( + $consumer->getOauthOptions()); + $docsService = new Zend_Gdata_Docs($httpClient, $APP_NAME); + $spreadsheetsService = new Zend_Gdata_Spreadsheets($httpClient, + $APP_NAME); + + // Retrieve user's list of Google Docs and spreadsheet list. + $docsFeed = $docsService->getDocumentListFeed(); + $spreadsheetFeed = $spreadsheetsService->getSpreadsheetFeed( + 'http://spreadsheets.google.com/feeds/spreadsheets/private/full?max-results=100'); + + renderHTML($accessToken, array($docsFeed, $spreadsheetFeed)); + } else { + renderHTML(); + } +} + +/** + * Returns a the base URL of the current running web app. + * + * @return string + */ +function getAppURL() { + $pageURL = 'http'; + if ($_SERVER['HTTPS'] == 'on') { + $pageURL .= 's'; + } + $pageURL .= '://'; + if ($_SERVER['SERVER_PORT'] != '80') { + $pageURL .= $_SERVER['SERVER_NAME'] . ':' . + $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF']; + } else { + $pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; + } + return $pageURL; +} + +/** + * Removes session data and redirects the user to a URL. + * + * @param string $redirectUrl The URL to direct the user to after session data + * is destroyed. + * @return void + */ +function logout($redirectUrl) { + session_destroy(); + header('Location: ' . $redirectUrl); + exit; +} + +/** + * Prints the token string and secret of the token passed in. + * + * @param Zend_OAuth_Token $token An access or request token object to print. + * @return void + */ +function printToken($token) { + echo '<b>Token:</b>' . $token->getToken() . '<br>'; + echo '<b>Token secret:</b>' . $token->getTokenSecret() . '<br>'; +} + +/** + * Prints basic properties of a Google Data feed. + * + * @param Zend_Gdata_Feed $feed A feed object to print. + * @return void + */ +function printFeed($feed) { + echo '<ol>'; + foreach ($feed->entries as $entry) { + $alternateLink = ''; + foreach ($entry->link as $link) { + if ($link->getRel() == 'alternate') { + $alternateLink = $link->getHref(); + } + } + echo "<li><a href=\"$alternateLink\" target=\"_new\">$entry->title</a></li>"; + } + echo '</ol>'; +} + +/** + * Renders the page's HTML. + * + * @param Zend_OAuth_Token $token (optional) The user's current OAuth token. + * @param array $feeds (optional) An array of Zend_Gdata_Feed to print + * information for. + * @return void + */ +function renderHTML($token=null, $feeds=null) { +?> +<!DOCTYPE html> +<html lang="en"> +<head> +<meta charset=utf-8 /> +<link href="style.css" type="text/css" rel="stylesheet"/> +</head> +<body> + <?php if (!isset($_SESSION['ACCESS_TOKEN'])) { ?> + <button onclick="location.href='<?php echo "$APP_URL?action=request_token" ?>';">Grant Access to this app!</button> + <?php } else { ?> + <div id="token_info"> + <span style="float:left;"><img src="http://code.google.com/apis/accounts/images/oauth_icon.png"></span> + <div id="token"><?php printToken($token); ?></div> + </div> + <div id="logout"><a href="<?php echo "$APP_URL?action=logout"; ?>">Logout</a></div> + <div style="clear:both;"> + <div id="doclist"> + <h4>First 100 documents from the <a href="http://code.google.com/apis/documents/" target="_new">Documents List Data API</a>:</h4> + <div class="feed"><?php printFeed($feeds[0]); ?></div> + </div> + <div id="spreadsheets"> + <h4>First 100 spreadsheets from the <a href="http://code.google.com/apis/spreadsheets/" target="_new">Spreadsheets Data API</a>:</h4> + <div class="feed"><?php printFeed($feeds[1]); ?></div> + </div> + </div> + <?php } ?> +</body> +</html> +<?php +} diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/style.css b/zend/demos/Zend/Gdata/3LeggedOAuth/style.css new file mode 100755 index 0000000..f572f60 --- /dev/null +++ b/zend/demos/Zend/Gdata/3LeggedOAuth/style.css @@ -0,0 +1,43 @@ +body { + font-family: Verdana, Arial, sans-serif; + font-size: 12px; +} +ul, ol { + padding: 0 0 0 15px; + margin: 0; +} +#logout { + text-align: right; +} +#token_info { + float: left; +} +#token { + margin-top: 12px; + padding: 10px 10px 10px 85px; + width: 500px; + border: 1px solid #ccc; + border-radius : 5px; + -moz-border-radius: 5px; + background-color: #eee; +} +#doclist, #spreadsheets { + float: left; + width: 450px; + margin-right: 10px; +} +#doclist .feed { + background: transparent url('doclist-72.png') no-repeat top right; +} +#spreadsheets .feed { + background: transparent url('data-api-72.png') no-repeat top right; +} +.feed { + border: 1px solid #ccc; + border-radius: 10px; + -moz-border-radius: 10px; + padding: 25px; +} +.feed h4 { + text-align: center; +} |
