} // Check to see if the access session handle ever expires. if ($oauth_authorization_expires_in) { $access_token->handleExpires = $now + $oauth_authorization_expires_in; } else { $access_token->handleExpires = -1; } return $access_token; } $foo = new OauthPanda(array('request_client' => new YahooCurlWrapper('../YahooCurl.class.php'), 'oauth_client' => new StandardOauthWrapper('../OAuth.php'), 'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, 'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET)); if (is_file('access_token.txt')) { //retrieve token $access_token = unserialize(file_get_contents('access_token.txt')); $url = "http://social.yahooapis.com/v1/users.guid({$access_token->guid})/profile"; $params = array('format' => 'json'); $response = $foo->set(array('oauth_params_location' => 'header', 'token' => $access_token))->GET(array('url' => $url, 'params' => $params)); $response_body = json_decode($response['response_body']); //if the token's expired, refresh it as per http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html#oauth-refreshaccesstoken if (401 == $response['http_code'] && false !== strpos($response_body->error->description, 'token_expired')) { $response = $foo->set(array('oauth_params_location' => 'url', 'token' => $access_token))->GET(array('url' => 'https://api.login.yahoo.com/oauth/v2/get_token', 'params' => array('oauth_session_handle' => $access_token->sessionHandle))); parse_str($response['response_body'], $access_token_data); $access_token = buildAccessTokenObject($access_token_data['oauth_token'], $access_token_data['oauth_token_secret'], $access_token_data['xoauth_yahoo_guid'], $access_token_data['oauth_session_handle'], $access_token_data['oauth_expires_in'], $access_token_data['oauth_authorization_expires_in']); file_put_contents('access_token.txt', serialize($access_token)); $response = $foo->set(array('oauth_params_location' => 'header', 'token' => $access_token))->GET(array('url' => $url, 'params' => $params)); $response_body = json_decode($response['response_body']); } //do something w/ data ... var_dump($response_body); } elseif (isset($_GET['oauth_verifier'])) { //fetch token $request_token = unserialize(file_get_contents('request_token.txt'));
static function test9() { require '../OauthPanda.class.php'; $foo = new OauthPanda(array('exception_handling' => 'throw', 'request_client' => new YahooCurlWrapper(), 'oauth_client' => new StandardOauthWrapper('../OAuth.php'), 'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, 'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET)); try { //calling BAR() would throw error, but deliberately setting method overrides $foo->set(array('request_client' => new YahooCurlWrapper('../YahooCurl.class.php')))->BAR(array('request_method' => 'GET', 'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token')); TestUtils::respond(__FUNCTION__, 'pass'); } catch (Exception $e) { TestUtils::assertTrue(false, 'deliberately setting request method should not throw exception<p/>' . print_r($e, true)); } }
<?php error_reporting(E_ALL); require '../private.php'; require '../OauthPanda.class.php'; //ref http://apiwiki.twitter.com/Twitter-API-Documentation $request_token_url = 'http://twitter.com/oauth/request_token'; $access_token_url = 'http://twitter.com/oauth/access_token'; $authentication_url = 'http://twitter.com/oauth/authenticate'; $foo = new OauthPanda(array('request_client' => new ShuberCurlWrapper('../../curl/curl.php'), 'oauth_client' => new StandardOauthWrapper('../OAuth.php'), 'consumer_key' => TWITTER_OAUTH_CONSUMER_KEY, 'consumer_secret' => TWITTER_OAUTH_CONSUMER_SECRET)); if (is_file('twitter_access_token.txt')) { //retrieve token $access_token = unserialize(file_get_contents('twitter_access_token.txt')); $url = sprintf("http://twitter.com/statuses/user_timeline/%s.json", $access_token->screen_name); $response = $foo->set(array('oauth_params_location' => 'url', 'token' => $access_token))->GET(array('url' => $url)); $response_body = json_decode($response->body); //do something w/ data ... var_dump($response_body); } elseif (isset($_GET['oauth_verifier'])) { //fetch token $request_token = unserialize(file_get_contents('twitter_request_token.txt')); //exchange request token for access token $response = $foo->set(array('oauth_params_location' => 'post', 'token' => $request_token))->POST(array('url' => $access_token_url, 'params' => array('oauth_verifier' => $_GET['oauth_verifier']))); parse_str($response->body, $access_token_data); //sanity check assert(isset($access_token_data['oauth_token'])); //format access token obj as expected by std oauth lib $access_token = new stdclass(); $access_token->key = $access_token_data['oauth_token']; $access_token->secret = $access_token_data['oauth_token_secret']; $access_token->user_id = $access_token_data['user_id'];