/** * Helper function to do 3-legged-OAuth OpenSocial request * @param $userId UserId to work with * @param $osapi_service.call Service to call * @param $keytoset name of the array key that will contain the results * @return array containing 'keytoset' => results, or osapiError-instance when error occurred */ protected function callOpenSocial($user_params, $osapi_service, $keytoset) { $storage = new osapiFileStorage($this->_filestoragepath); $auth = osapiOAuth3Legged_10a::performOAuthLogin($this->_consumerkey, $this->_consumersecret, $storage, $this->_osapiProvider, $user_params['userId']); $osapi = new osapi($this->_osapiProvider, $auth); if ($this->_strictMode) { $osapi->setStrictMode($strictMode); } // Start a batch so that many requests may be made at once. $batch = $osapi->newBatch(); $call = explode('.', $osapi_service); if (sizeof($call) != 2) { throw new Exception("Invalid OpenSocial service call: {$osapi_service}"); } // Instantiate service $oService = $osapi->{$call}[0]; $user_params['userId'] = '@me'; // real userId does not work,use '@me' instead $batch->add($oService->{$call}[1]($user_params), $keytoset); // Send the batch request. try { $result = $batch->execute(); } catch (Exception $e) { // Rethrow throw new Exception("Invalid OSAPI-call: " . $e->getMessage()); } //print_r($result); //print_r($oService); exit(); if ($result[$keytoset] instanceof osapiError) { $err = $result[$keytoset]; if ($err->getErrorCode() == 401) { // Token did not authorize the request; dispose of it, and // get a new one: if (($token = $storage->get($auth->storageKey)) !== false) { $storage->delete($auth->storageKey); /* protect against infinite local loop */ $this->_token_retry_count = isset($this->_token_retry_count) ? $this->_token_retry_count + 1 : 1; if ($this->_token_retry_count < 3) { $this->prepareClient($user_params['userId']); return $this->callOpenSocial($user_params, $osapi_service, $keytoset); } else { throw new Exception("Could not establish accesstoken"); } } else { throw new Exception("Problem occured when performing OpenSocial call: {$osapi_service}"); } } } return $result; }
<?php // Require the osapi library require_once "../src/osapi.php"; $consumerKey = 'CONSUMER_KEY'; $consumerSecret = 'CONSUMER_SECRET'; $callbackUrl = 'CALLBACK_UR:'; $storage = new osapiFileStorage('/tmp/osapi'); $provider = new osapiVzOAuthProvider(osapiVzOAuthProvider::STUDIVZ); $auth = osapiOAuth3Legged_10a::performOAuthLogin($consumerKey, $consumerSecret, $storage, $provider); $osapi = new osapi($provider, $auth); // Enable logger. osapiLogger::setLevel(osapiLogger::INFO); osapiLogger::setAppender(new osapiConsoleAppender()); // Start a batch so that many requests may be made at once. $batch = $osapi->newBatch(); // Fetch the current user. $self_request_params = array('userId' => '@me', 'groupId' => '@self', 'fields' => array()); $batch->add($osapi->people->get($self_request_params), 'self'); $result = $batch->execute(); var_dump($result);