/**
  * The 3 legged oauth class needs a way to store the access key and token
  * it uses the osapiStorage class to do so.
  *
  * Constructing this class will initiate the 3 legged oauth work flow, including redirecting
  * to the OAuth provider's site if required(!)
  *
  * @param string $consumerKey
  * @param string $consumerSecret
  * @param osapiStorage $storage storage class to use (file,apc,memcache,mysql)
  * @param osapiProvider $provider the provider configuration (required to get the oauth endpoints)
  * @param any $localUser the *local* user ID (this is not the user's ID on the social network site, but the user id on YOUR site, this is used to link the oauth access token to a local login)
  * @param any $userId the *remote* user ID, you can supply this user id if known but it's completely optional. If set it will be included in the oauth requests in the xoauth_requestor_id field)
  * @return osapiOAuth3Legged the logged-in provider instance
  */
 public static function performOAuthLogin($consumerKey, $consumerSecret, osapiStorage $storage, osapiProvider $provider, $localUserId = null, $userId = null)
 {
     $auth = new osapiOAuth3Legged_10a($consumerKey, $consumerSecret, $storage, $provider, $localUserId, $userId);
     if (($token = $storage->get($auth->storageKey)) !== false) {
         $auth->accessToken = $token;
     } else {
         if (isset($_GET['oauth_verifier']) && isset($_GET['oauth_token']) && isset($_GET['uid'])) {
             $uid = $_GET['uid'];
             $secret = $auth->storage->get($auth->storageKey . ":nonce" . $uid);
             $auth->storage->delete($auth->storageKey . ":nonce" . $uid);
             $token = $auth->upgradeRequestToken($_GET['oauth_token'], $secret, $_GET['oauth_verifier']);
             $auth->redirectToOriginal();
         } else {
             // Initialize the OAuth dance, first request a request token, then kick the client to the authorize URL
             // First we store the current URL in our storage, so that when the oauth dance is completed we can return there
             $callbackUrl = ($_SERVER['HTTPS'] == 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             $uid = uniqid();
             $token = $auth->obtainRequestToken($callbackUrl, $uid);
             // print_r($token); exit();
             $auth->storage->set($auth->storageKey . ":nonce" . $uid, $token->secret);
             $auth->redirectToAuthorization($token);
         }
     }
     return $auth;
 }
 /**
  * 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);