/** * Make a call using Oauth client * @param String */ protected function request($url, $method = Zend_Http_Client::GET) { $http = $this->token->getHttpClient($this->oauthOptions); $http->setUri($url); $http->setMethod($method); switch ($method) { case Zend_Http_Client::PUT: //$this->client->setParameterPost('access_token',$this->access_token); //$this->client->setEncType(self::ENC_FORMDATA); break; case Zend_Http_Client::POST: //$this->client->setParameterPost('access_token', $this->access_token); break; case Zend_Http_Client::GET: case Zend_Http_Client::DELETE: default: //$this->client->setParameterGet('access_token',$this->access_token); break; } try { $response = $http->request(); $data = $response->getBody(); } catch (Zend_Http_Client_Exception $e) { // Return Exception as JSON $data = new stdClass(); $data->code = $e->getCode(); $data->error = $e->getMessage(); } return $data; }
private function _getClient($uri) { $client = new Zend_Http_Client('http://api.dropbox.com/0/token'); $client->setParameterPost(array( 'oauth_consumer_key' => self::DB_CONSUMER_KEY, 'oauth_consumer_secret' => self::DB_CONSUMER_SECRET, 'email' => self::DB_USER, 'password' => self::DB_PASSWORD, )); $response = $client->request('POST'); $data = json_decode($response->getBody()); $token = $data->token; $secret = $data->secret; $tokenObject = new Zend_Oauth_Token_Access(); $tokenObject->setToken($token); $tokenObject->setTokenSecret($secret); $options = array( 'consumerKey' => self::DB_CONSUMER_KEY, 'consumerSecret' => self::DB_CONSUMER_SECRET, ); return $tokenObject->getHttpClient($options, $uri); }
/** * Authenticate to the Google Docs API using OAuth. * * @param string $consumerKey The OAuth consumer key used to authenticate to Google. * @param string $consumerSecret The OAuth consumer secret used to authenticate to Google. * * @return Zend_Oauth_Client object */ function AuthenticateToGoogle($consumerKey, $consumerSecret) { $oauthOptions = array('requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER, 'version' => '1.0', 'signatureMethod' => 'HMAC-SHA1', 'consumerKey' => $consumerKey, 'consumerSecret' => $consumerSecret); //$consumer = new Zend_Oauth_Consumer($oauthOptions); $token = new Zend_Oauth_Token_Access(); $httpClient = $token->getHttpClient($oauthOptions); return $httpClient; }
public function revoke() { if ($this->_accessToken && $this->_accessToken instanceof Zend_Oauth_Token_Access) { $client = $this->_accessToken->getHttpClient($this->_options); // Set LinkedIn URI $client->setUri($this->_options['invalidateTokenUrl']); $client->setMethod(Zend_Http_Client::GET); $response = $client->request(); return $response->getMessage() == 'OK' ? true : false; } }
/** * @method oAuth * @static * @param {string} $provider Currently only supports the value "facebook". * @return {Zend_Oauth_Client} * @throws {Users_Exception_NotLoggedIn} If user is not logged in */ static function oAuth($provider) { $nativeuser = self::loggedInUser(); if (!$nativeuser) { throw new Users_Exception_NotLoggedIn(); } #Set up oauth options $oauthOptions = Q_Config::expect('Users', 'oAuthProviders', $provider, 'oAuth'); $customOptions = Q_Config::get('Users', 'oAuthProviders', $provider, 'custom', null); #If the user already has a token in our DB: $appuser = new Users_AppUser(); $appuser->userId = $nativeuser->id; $appuser->provider = $provider; $appuser->appId = Q_Config::expect('Users', 'oAuthProviders', $provider, 'appId'); if ($appuser->retrieve('*', true)) { $zt = new Zend_Oauth_Token_Access(); $zt->setToken($appuser->access_token); $zt->setTokenSecret($appuser->session_secret); return $zt->getHttpClient($oauthOptions); } #Otherwise, obtain a token from provider: $consumer = new Zend_Oauth_Consumer($oauthOptions); if (isset($_GET['oauth_token']) && isset($_SESSION[$provider . '_request_token'])) { $token = $consumer->getAccessToken($_GET, unserialize($_SESSION[$provider . '_request_token'])); $_SESSION[$provider . '_access_token'] = serialize($token); $_SESSION[$provider . '_request_token'] = null; #Save tokens to database $appuser->access_token = $token->getToken(); $appuser->session_secret = $token->getTokenSecret(); $appuser->save(); return $token->getHttpClient($oauthOptions); } else { $token = $consumer->getRequestToken($customOptions); $_SESSION[$provider . '_request_token'] = serialize($token); $consumer->redirect(); return null; } }
public function __construct($username, $config, Zend_Oauth_Token_Access $token) { $this->_authInitialized = true; $this->_client = $token->getHttpClient($config); self::setHttpClient($this->_client); parent::__construct($username, null); }
/** * Fetches a secured oauth url and returns the response body. * * @param string $uri * @param mixed $arguments * @param string $method * @param array $httpHeaders * @return string */ public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) { $token = $this->OAuth->getToken(); if (!is_a($token, "Zend_Oauth_Token")) { if (is_a($this->zend_oauth_token, "Zend_Oauth_Token_Access")) { $token = $this->zend_oauth_token; } else { $token = new Zend_Oauth_Token_Access(); $token->setToken($this->oauth_token); $token->setTokenSecret($this->oauth_token_secret); } } /* @var $token Zend_Oauth_Token_Access */ $oauthOptions = array('consumerKey' => $this->consumerKey, 'signatureMethod' => "HMAC-SHA1", 'consumerSecret' => $this->OAuth->getConsumerSecret()); $config = array("timeout" => 15); /* @var $consumerRequest Zend_Oauth_Client */ $consumerRequest = $token->getHttpClient($oauthOptions); $consumerRequest->setMethod($method); if (is_array($arguments)) { $consumerRequest->setUri($uri); if ($method == "GET") { foreach ($arguments as $param => $value) { $consumerRequest->setParameterGet($param, $value); } } else { foreach ($arguments as $param => $value) { $consumerRequest->setParameterPost($param, $value); } } } elseif (is_string($arguments)) { preg_match("/\\?file=(.*)\$/i", $uri, $matches); if (isset($matches[1])) { $uri = str_replace($matches[0], "", $uri); $filename = $matches[1]; $uri = Zend_Uri::factory($uri); $uri->addReplaceQueryParameters(array("file" => $filename)); $consumerRequest->setParameterGet("file", $filename); } $consumerRequest->setUri($uri); $consumerRequest->setRawData($arguments); } elseif (is_resource($arguments)) { $consumerRequest->setUri($uri); /** Placeholder for Oauth streaming support. */ } if (count($httpHeaders)) { foreach ($httpHeaders as $k => $v) { $consumerRequest->setHeaders($k, $v); } } $response = $consumerRequest->request(); $body = Zend_Json::decode($response->getBody()); switch ($response->getStatus()) { // Not modified case 304: return array('httpStatus' => 304, 'body' => null); break; case 403: throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location. ' . $body["error"] . "\n"); case 404: throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found. ' . $body["error"] . "\n"); case 507: throw new Dropbox_Exception_OverQuota('This dropbox is full. ' . $body["error"] . "\n"); } return array('httpStatus' => $response->getStatus(), 'body' => $response->getBody()); }
// This is the *ACCESS* Token and Secret. You should store these in your // database with the user's record. We're putting them in the session only // so the demo will work. $prev_token = $_SESSION['token']; $_SESSION['token'] = $access_token->getToken(); $_SESSION['token_secret'] = $access_token->getTokenSecret(); if (strlen($_SESSION['token']) && strlen($_SESSION['token_secret']) && $_SESSION['token'] != $prev_token) { echo "Step 8: Success! Your final access token is {$_SESSION['token']}. "; echo "We can now proceed to step nine. "; echo '<a href="', $_SERVER['PHP_SELF'], '">Clicky.</a>'; $_SESSION['oauth_state'] = 2; } else { echo "Something went wrong. Didn't get the right tokens. You should probably see an error message above.<br>"; echo "Be aware that these tokens are one-use only. You <i>might</i> need to reset your session."; } exit; } elseif ($_SESSION['oauth_state'] == 2) { echo "Step 9: You should have access with key {$_SESSION['token']}!<br>"; // Once again, this is why they have you serialize it in their example code... $access_token = new Zend_Oauth_Token_Access(); $access_token->setToken($_SESSION['token']); $access_token->setTokenSecret($_SESSION['token_secret']); // setOAuth wants to work only with the result of this call, which will be // a Zend_Oauth_Client, a subclass of Zend_Http_Client. Imgur::setOAuth($access_token->getHttpClient($zend_oauth_config)); // We'll fall through at this point, so the demo in oauth.php can run. echo "Done! We can make make OAuth requests on your behalf.<br><hr>"; } else { echo "Whoa, your OAuth state is totally bogus, dude."; exit; }
/** * */ protected static function request($customernumber, $endpoint) { static $zendready = false; if (!$zendready) { $dir = get_config('core', 'dirroot'); set_include_path(get_include_path() . PATH_SEPARATOR . $dir . '/blocks/mhaairs/lib'); $zendready = true; } // @codingStandardsIgnoreStart require_once 'Zend/Json.php'; require_once 'Zend/Oauth/Consumer.php'; require_once 'Zend/Oauth/Client.php'; // @codingStandardsIgnoreEnd $baseurl = 'http://mhaairs.tegrity.com/v1/Config/'; $url = $baseurl . $customernumber . '/' . $endpoint; $aconfig = array('requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING, 'requestMethod' => Zend_Oauth::GET, 'signatureMethod' => 'HMAC-SHA1', 'consumerKey' => 'SSOConfig', 'consumerSecret' => '3DC9C384'); $resultdata = false; try { $tacc = new Zend_Oauth_Token_Access(); $client = $tacc->getHttpClient($aconfig, $url); $client->setMethod(Zend_Oauth_Client::GET); $client->setEncType(Zend_Oauth_Client::ENC_URLENCODED); $response = $client->request(); $resultdata = $response->getBody(); // Get content type. $resulttype = $response->getHeader(Zend_Oauth_Client::CONTENT_TYPE); // Is this Json encoded data? if (stripos($resulttype, 'application/json') !== false) { $resultdata = Zend_Json::decode($resultdata); } // By default set the status to the HTTP response status. $status = $response->getStatus(); $description = $response->getMessage(); if ($status != 200) { $resultdata = false; } } catch (Exception $e) { $status = (string) $e->getCode(); $description = $e->getMessage(); } $logmsg = $status . ": " . $description; // TODO // There used to be add_to_log here recording the requested endpoint and log message. // This should be replaced with events system as soon as we decide what should be // logged. return $resultdata; }