From 06f945f27840b53e57795dadbc38e76f7e11ab1c Mon Sep 17 00:00:00 2001 From: Horus3 Date: Mon, 24 Feb 2014 16:42:14 +0100 Subject: init --- .../Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php | 109 ++++++++++++ zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png | Bin 0 -> 5294 bytes zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png | Bin 0 -> 5696 bytes zend/demos/Zend/Gdata/3LeggedOAuth/index.php | 190 +++++++++++++++++++++ zend/demos/Zend/Gdata/3LeggedOAuth/style.css | 43 +++++ 5 files changed, 342 insertions(+) create mode 100755 zend/demos/Zend/Gdata/3LeggedOAuth/Gdata_OAuth_Helper.php create mode 100755 zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png create mode 100755 zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png create mode 100755 zend/demos/Zend/Gdata/3LeggedOAuth/index.php create mode 100755 zend/demos/Zend/Gdata/3LeggedOAuth/style.css (limited to 'zend/demos/Zend/Gdata/3LeggedOAuth') 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 @@ + 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 new file mode 100755 index 0000000..e77c523 Binary files /dev/null and b/zend/demos/Zend/Gdata/3LeggedOAuth/data-api-72.png differ diff --git a/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png b/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png new file mode 100755 index 0000000..39de3a1 Binary files /dev/null and b/zend/demos/Zend/Gdata/3LeggedOAuth/doclist-72.png differ 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 @@ +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 'Token:' . $token->getToken() . '
'; + echo 'Token secret:' . $token->getTokenSecret() . '
'; +} + +/** + * Prints basic properties of a Google Data feed. + * + * @param Zend_Gdata_Feed $feed A feed object to print. + * @return void + */ +function printFeed($feed) { + echo '
    '; + foreach ($feed->entries as $entry) { + $alternateLink = ''; + foreach ($entry->link as $link) { + if ($link->getRel() == 'alternate') { + $alternateLink = $link->getHref(); + } + } + echo "
  1. $entry->title
  2. "; + } + echo '
'; +} + +/** + * 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) { +?> + + + + + + + + + + +
+ +
+
+
">Logout
+
+
+

First 100 documents from the Documents List Data API:

+
+
+
+

First 100 spreadsheets from the Spreadsheets Data API:

+
+
+
+ + + +