* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
include_once "../../library/OAuthStore.php";
include_once "../../library/LingotekOAuthRequester.php";
// register at http://twitter.com/oauth_clients and fill these two
define("TWITTER_CONSUMER_KEY", "FILL THIS");
define("TWITTER_CONSUMER_SECRET", "FILL THIS");
define("TWITTER_OAUTH_HOST", "https://twitter.com");
define("TWITTER_REQUEST_TOKEN_URL", TWITTER_OAUTH_HOST . "/oauth/request_token");
define("TWITTER_AUTHORIZE_URL", TWITTER_OAUTH_HOST . "/oauth/authorize");
define("TWITTER_ACCESS_TOKEN_URL", TWITTER_OAUTH_HOST . "/oauth/access_token");
define("TWITTER_PUBLIC_TIMELINE_API", TWITTER_OAUTH_HOST . "/statuses/public_timeline.json");
define("TWITTER_UPDATE_STATUS_API", TWITTER_OAUTH_HOST . "/statuses/update.json");
define('OAUTH_TMP_DIR', function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : realpath($_ENV["TMP"]));
// Twitter test
$options = array('consumer_key' => TWITTER_CONSUMER_KEY, 'consumer_secret' => TWITTER_CONSUMER_SECRET);
OAuthStore::instance("2Leg", $options);
try {
    // Obtain a request object for the request we want to make
    $request = new LingotekOAuthRequester(TWITTER_REQUEST_TOKEN_URL, "POST");
    $result = $request->doRequest(0);
    parse_str($result['body'], $params);
    echo "aa";
    // now make the request.
    $request = new LingotekOAuthRequester(TWITTER_PUBLIC_TIMELINE_API, 'GET', $params);
    $result = $request->doRequest();
} catch (OAuthException2 $e) {
    echo "Exception" . $e->getMessage();
}
 /**
  * Request an access token from the site belonging to consumer_key.
  * Before this we got an request token, now we want to exchange it for
  * an access token.
  * 
  * @param string consumer_key
  * @param string token
  * @param int usr_id		user requesting the access token
  * @param string method (optional) change the method of the request, defaults to POST (as it should be)
  * @param array options (optional) extra options for request, eg token_ttl
  * @param array curl_options	optional extra options for curl request
  *  
  * @exception OAuthException2 when no key could be fetched
  * @exception OAuthException2 when no server with consumer_key registered
  */
 static function requestAccessToken($consumer_key, $token, $usr_id, $method = 'POST', $options = array(), $curl_options = array())
 {
     LingotekOAuthRequestLogger::start();
     $store = OAuthStore::instance();
     $r = $store->getServerTokenSecrets($consumer_key, $token, 'request', $usr_id);
     $uri = $r['access_token_uri'];
     $token_name = $r['token_name'];
     // Delete the server request token, this one was for one use only
     $store->deleteServerToken($consumer_key, $r['token'], 0, true);
     // Try to exchange our request token for an access token
     $oauth = new LingotekOAuthRequester($uri, $method);
     if (isset($options['oauth_verifier'])) {
         $oauth->setParam('oauth_verifier', $options['oauth_verifier']);
     }
     if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
         $oauth->setParam('xoauth_token_ttl', intval($options['token_ttl']));
     }
     LingotekOAuthRequestLogger::setRequestObject($oauth);
     $oauth->sign($usr_id, $r, '', 'accessToken');
     $text = $oauth->curl_raw($curl_options);
     if (empty($text)) {
         throw new OAuthException2('No answer from the server "' . $uri . '" while requesting an access token');
     }
     $data = $oauth->curl_parse($text);
     if ($data['code'] != 200) {
         throw new OAuthException2('Unexpected result from the server "' . $uri . '" (' . $data['code'] . ') while requesting an access token');
     }
     $token = array();
     $params = explode('&', $data['body']);
     foreach ($params as $p) {
         @(list($name, $value) = explode('=', $p, 2));
         $token[$oauth->urldecode($name)] = $oauth->urldecode($value);
     }
     if (!empty($token['oauth_token']) && !empty($token['oauth_token_secret'])) {
         $opts = array();
         $opts['name'] = $token_name;
         if (isset($token['xoauth_token_ttl'])) {
             $opts['token_ttl'] = $token['xoauth_token_ttl'];
         }
         $store->addServerToken($consumer_key, 'access', $token['oauth_token'], $token['oauth_token_secret'], $usr_id, $opts);
     } else {
         throw new OAuthException2('The server "' . $uri . '" did not return the oauth_token or the oauth_token_secret');
     }
     LingotekOAuthRequestLogger::flush();
 }