Skip to content

Instantly share code, notes, and snippets.

@erikeldridge
Created May 18, 2010 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikeldridge/404714 to your computer and use it in GitHub Desktop.
Save erikeldridge/404714 to your computer and use it in GitHub Desktop.
a php script that uses the standard oauth lib to do the oauth dance
<?php // a php script that uses the standard oauth lib (via yos sdk) to do the oauth dance
/**
* Requirements:
* - PHP5
* - YOS PHP5 SDK: http://github.com/yahoo/yos-social-php5
*/
require('../yosdk/yahoo-yos-social-php5-86eef28/lib/OAuth/OAuth.php');
require('../yosdk/yahoo-yos-social-php5-86eef28/lib/Yahoo/YahooOAuthApplication.class.php');
// we've got a stored access token
if ( $_REQUEST['serialized_access_token'] ) {
$access_token = json_decode( stripslashes( $_REQUEST['serialized_access_token'] ) );
printf('<pre>%s</pre>', print_r($access_token, true));
// we're on the callback
} elseif ( $_REQUEST['serialized_request_token'] && $_REQUEST['oauth_verifier'] ) {
$app = new YahooOauthApplication( $_REQUEST['key'], $_REQUEST['secret'], null, $_REQUEST['callback'] );
$request_token = json_decode( stripslashes( $_REQUEST['serialized_request_token'] ) );
$access_token = $app->getAccessToken( $request_token, $_REQUEST['oauth_verifier'] );
// clear req token
setcookie('serialized_request_token', '', time()-3600);
// cache access token
setcookie('serialized_access_token', json_encode( $access_token ) );
printf('<pre>%s</pre>', print_r($access_token, true));
// we just submitted the form
} elseif( $_REQUEST['submit'] ){
$app = new YahooOauthApplication( $_REQUEST['key'], $_REQUEST['secret'], null, $_REQUEST['callback'] );
$token = $app->getRequestToken( $_REQUEST['callback'] );
// cache params & token for 2nd step
setcookie('key', $_REQUEST['key'] );
setcookie('secret', $_REQUEST['secret'] );
setcookie('callback', $_REQUEST['callback'] );
setcookie('serialized_request_token', json_encode($token));
$url = $app->getAuthorizationUrl( $token );
header("Location: $url");
exit;
}
?>
<? if( !$_REQUEST['serialized_access_token'] && !$access_token ): ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.1/build/reset-fonts-grids/reset-fonts-grids.css">
<style>
body {
padding: 20px;
}
button {
float: left;
background-color: #fff;
padding: 1ex;
margin: 2ex 0;
}
label {
display: block;
text-align: left;
width: 10em;
}
input {
float: left;
width: 64em;
padding: 1ex;
margin: 2ex 0;
}
#submit {
width: 7em;
}
</style>
</head>
<body>
<form>
<div>
<label>Consumer key:</label><input name="key" value="">
<div style="clear:both"/>
</div>
<div>
<label>Consumer secret:</label><input name="secret" value="">
<div style="clear:both"/>
</div>
<div>
<label>Callback URL:</label><input name="callback" value="">
<div style="clear:both"/>
</div>
<input value="Authorize" name="submit" type="submit" id="submit">
</form>
</body>
</html>
<? endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment