spl_autoload_register('culturefeed_autoload');
try {
    $endpoint = $_SERVER['argv'][1];
    $consumer_key = $_SERVER['argv'][2];
    $consumer_secret = $_SERVER['argv'][3];
    $callback_url = $_SERVER['argv'][4];
    $oc = new CultureFeed_DefaultOAuthClient($consumer_key, $consumer_secret);
    $oc->setEndpoint($endpoint);
    $c = new CultureFeed($oc);
    $token = $c->getRequestToken($callback_url);
    print "Requested oauth_token: {$token['oauth_token']}" . PHP_EOL;
    // Save the token and secret in the session.
    //$_SESSION['oauth_token'] = $token['oauth_token'];
    //$_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
    // Fetch the authorisation url...
    $auth_url = $c->getUrlAuthorize($token, $callback_url, CultureFeed::AUTHORIZE_TYPE_REGULAR);
    print "Now open the following URL in your browser: {$auth_url}" . PHP_EOL;
    print "After logging in with your UiTID you should inspect your RequestBin and find there the last request with an oauth_token and oauth_verifier." . PHP_EOL;
    print "Make sure the you have identified the right request, containing the oauth token: {$token['oauth_token']}" . PHP_EOL;
    print "Give me the oauth_verifier value now: ";
    $oauth_verifier = fgets(STDIN);
    $oauth_verifier = trim($oauth_verifier);
    $user_oauth_client = new CultureFeed_DefaultOAuthClient($consumer_key, $consumer_secret, $token['oauth_token'], $token['oauth_token_secret']);
    $user_oauth_client->setEndpoint($endpoint);
    $user_c = new CultureFeed($user_oauth_client);
    $new_token = $user_c->getAccessToken($oauth_verifier);
    $new_user_oauth_client = new CultureFeed_DefaultOAuthClient($consumer_key, $consumer_secret, $new_token['oauth_token'], $new_token['oauth_token_secret']);
    $new_user_oauth_client->setEndpoint($endpoint);
    $new_user_c = new CultureFeed($new_user_oauth_client);
    $account = $new_user_c->getUser($new_token['userId']);
    print "You have succesfully logged in with the following account: {$account->nick} [{$account->id}]" . PHP_EOL;