Skip to content

Instantly share code, notes, and snippets.

@thefinn93
Created August 24, 2012 01:04
Show Gist options
  • Save thefinn93/3444268 to your computer and use it in GitHub Desktop.
Save thefinn93/3444268 to your computer and use it in GitHub Desktop.
Reddit Oauth
<?
require_once("../requests/library/Requests.php"); // Get this from http://requests.ryanmccue.info/
Requests::register_autoloader();
$client_id = ""; // Get these from https://ssl.reddit.com/prefs/apps
$client_secret = "";
$code = $_GET['code'];
$access_token_url = "https://oauth.reddit.com/api/v1/access_token";
$options = array('auth' => array($client_id, $client_secret));
$token_request = Requests::post($access_token_url, array(), array("grant_type" => "authorization_code","code" => $code, "redirect_uri" => "https://www.thefinn93.com/reddit/cb"), $options);
$response = json_decode($token_request->body);
if(!isset($response->error)) {
$headers = array("Authorization" => "bearer ".$response->access_token);
$me = Requests::get("https://oauth.reddit.com/api/v1/me.json",$headers); // Go ahead and request whatever you want here, nice list at https://github.com/reddit/reddit/wiki
$user = json_decode($me->body);
echo "Hello <a href=\"http://www.reddit.com/user/".$user->name."\">".$user->name.</a>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment