Skip to content

Instantly share code, notes, and snippets.

@ritou
Created February 5, 2011 16:21
Show Gist options
  • Save ritou/812557 to your computer and use it in GitHub Desktop.
Save ritou/812557 to your computer and use it in GitHub Desktop.
pecl_oauth_yahoojp_sample
<?php
// Request Token Endpoint
$req_url = 'https://auth.login.yahoo.co.jp/oauth/v2/get_request_token';
// AuthZ Endpoint
$authurl = 'https://auth.login.yahoo.co.jp/oauth/v2/request_auth';
// Access Token Endpoint
$acc_url = 'https://auth.login.yahoo.co.jp/oauth/v2/get_token';
// callback_url
$cbc_url = 'Input Callback URL';
// sample API
$api_url = 'http://auctions.yahooapis.jp/AuctionWebService/V2/openWatchList';
// Consumerkey
$conskey = 'Input Consumer key';
// Consumersecret
$conssec = 'Input Consumer Secret';
session_name("pecloauthyjp");
session_start();
if(isset($_GET['clear']) && $_GET['clear']==1){
$_SESSION=array();
}
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0;
try {
$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
if(!isset($_GET['oauth_token']) && !$_SESSION['state'] || $_SESSION['state']==0 ) {
$request_token_info = $oauth->getRequestToken($req_url,$cbc_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authurl.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url,'',$_GET['oauth_verifier']);
$token_res = var_export($access_token_info, true);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
$_SESSION['osh'] = $access_token_info['oauth_session_handle'];
$_SESSION['guid'] = $access_token_info['xoauth_yahoo_guid'];
}
if(isset($_GET['refresh']) && $_GET['refresh']==1){
// update Access Token
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($acc_url, $_SESSION['osh']);
$token_res = var_export($access_token_info, true);
}
// API Access
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
$oauth->fetch($api_url);
$res = $oauth->getLastResponse();
} catch(OAuthException $E) {
print_r($E);
}
?>
<html>
<head>
<title>PECL OAuth Library Sample with Yahoo! JAPAN</title>
</head>
<body>
<h1>PECL OAuth Library Sample with Yahoo! JAPAN</h1>
<a href="./">reload</a>
<a href="./?refresh=1">Access Token Refresh</a>
<a href="./?clear=1">restart</a>
<p>Access Token Response : </p>
<pre><?php echo htmlspecialchars(@$token_res); ?></pre>
<p>API Response : </p>
<pre><?php echo htmlspecialchars(@$res); ?></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment