/**
  * 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($consumerKey, $consumerSecret, $storage, $provider, $localUserId, $userId);
     if (($token = $storage->get($auth->storageKey)) !== false) {
         $auth->accessToken = $token;
     } else {
         if (isset($_GET['oauth_continue'])) {
             $token = $auth->upgradeRequestToken($_GET['token'], $_GET['key']);
             $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 = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             $token = $auth->obtainRequestToken($callbackUrl);
             $callbackUrl .= (strpos($_SERVER['REQUEST_URI'], '?') !== false ? '&' : '?') . 'oauth_continue=1&token=' . $token->key . '&key=' . urldecode($token->secret);
             $auth->redirectToAuthorization($token, $callbackUrl);
         }
     }
     return $auth;
 }
 /**
  * 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($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 = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             $uid = uniqid();
             $token = $auth->obtainRequestToken($callbackUrl, $uid);
             $auth->storage->set($auth->storageKey . ":nonce" . $uid, $token->secret);
             $auth->redirectToAuthorization($token);
         }
     }
     return $auth;
 }
 */
// get current session id
$session_id = session_id();
// enable osapi logging to file
osapiLogger::setLevel(osapiLogger::INFO);
osapiLogger::setAppender(new osapiFileAppender(sys_get_temp_dir() . '/opensocial.log'));
// create yahoo open social provider
$provider = new osapiYahooProvider();
// create file system storage using system temp directory
$storage = new osapiFileStorage(sys_get_temp_dir());
// if this is a YAP application, the access token and secret
// will be provided.
if (isset($_POST['yap_viewer_access_token']) && isset($_POST['yap_viewer_access_token_secret']) && isset($_POST['yap_viewer_guid'])) {
    $oauth = new osapiOAuth3Legged(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $storage, $provider, $_POST['yap_viewer_guid'], $_POST['yap_viewer_guid'], $_POST['yap_viewer_access_token'], $_POST['yap_viewer_access_token_secret']);
} else {
    $oauth = osapiOAuth3Legged::performOAuthLogin(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $storage, $provider, $session_id);
}
// create open social instance from yahoo provider + oauth credentials
$opensocial = new osapi($provider, $oauth);
// The number of friends to fetch.
$friend_count = 10;
// Start a batch so that many requests may be made at once.
$batch = $opensocial->newBatch();
// Fetch the user profile
$batch->add($opensocial->people->get(array('userId' => '@me', 'groupId' => '@self', 'fields' => array('displayName'))), 'self');
// Fetch the friends of the user
$batch->add($opensocial->people->get(array('userId' => '@me', 'groupId' => '@friends', 'fields' => array('id'), 'count' => 100)), 'friends');
// Request the activities of the current user
$batch->add($opensocial->activities->get(array('userId' => '@me', 'groupId' => '@self', 'count' => 100)), 'userActivities');
// Send the batch request
$result = $batch->execute();
    case 'myspace':
        $userId = '495184236';
        $osapi = new osapi(new osapiMySpaceProvider(), new osapiOAuth2Legged("http://www.myspace.com/495182150", "20ab52223e684594a8050a8bfd4b06693ba9c9183ee24e1987be87746b1b03f8", $userId));
        break;
    case 'myspaceid':
        // NOTE: to authorize using 3leggedOAuth & MySpace your app MUST be a MySpaceID app otherwise use 2LeggedOAuth
        $provider = new osapiMySpaceProvider();
        $storage = new osapiFileStorage('/tmp/osapi');
        $auth = osapiOAuth3Legged::performOAuthLogin('6043e2bda2e546498928732c01982b24', '14d70750b3ae45c1b8c8b834f145aa3fa632013885964cf6bd41792d51754810', $storage, $provider, $localUserId);
        $osapi = new osapi($provider, $auth);
        break;
    case 'google3legged':
        // See http://code.google.com/apis/accounts/docs/OAuthForInstalledApps.html for information about the key/secret
        $provider = new osapiGoogleProvider();
        $storage = new osapiFileStorage('/tmp/osapi');
        $auth = osapiOAuth3Legged::performOAuthLogin('anonymous', 'anonymous', $storage, $provider, $localUserId);
        $osapi = new osapi($provider, $auth);
        break;
    case 'google2legged':
        $userId = '101911127807751034357';
        $provider = new osapiGoogleProvider();
        $provider->rpcEndpoint = null;
        $osapi = new osapi($provider, new osapiOAuth2Legged("google.com:249475676706", "fWPcoVP6DOLVqZOF2HH+ihU2", $userId));
        break;
}
$script_name = $_SERVER["SCRIPT_NAME"];
$tests = array("google2legged" => "Google (2 legged)", "google3legged" => "Google (3 legged)", "myspace" => "MySpace", "myspaceid" => "MySpaceID", "orkut" => "orkut", "orkutRest" => "orkut (REST)", "partuza" => "Partuza", "partuzaLocal" => "partuzaLocal", "partuzaLocalRest" => "partuzaLocalRest", "plaxo" => "Plaxo", "netlog" => 'Netlog', 'hi5' => 'Hi5');
$links = array();
foreach ($tests as $value => $name) {
    if ($value == $test) {
        $links[] = "<strong>{$name}</strong>";