Created
September 20, 2012 18:52
-
-
Save thefinn93/3757656 to your computer and use it in GitHub Desktop.
Reddit Oauth in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
session_start(); | |
if(isset($_REQUEST['state'])) { | |
if($_REQUEST['state'] != $_SESSION['state']) { | |
die("Oy, <a href=\"/reddit\">get yourself a proper state token, noob</a>"); | |
} | |
} else { | |
die("Oy, <a href=\"/reddit\">get yourself a state token, noob</a>"); | |
} | |
require_once("../requests/library/Requests.php"); | |
Requests::register_autoloader(); | |
$code = $_GET['code']; | |
$client_id = "hax"; | |
$client_secret = "morhax"; | |
$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" => "PATH TO REDIRECT URL"), $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); | |
$user = json_decode($me->body); | |
echo "Hello <a href=\"http://www.reddit.com/user/".$user->name."\">".$user->name."</a><br />"; | |
echo "Attempting to post..."; | |
$submitdata = array("thing_id" => "t3_yqfb4","text" => "Wangs. Wangs wangs wangs. ".uniqid()); | |
$post = Requests::post("https://oauth.reddit.com/api/comment",$headers,$submitdata); | |
$result = json_decode($post->body); | |
echo "<pre>"; | |
print_r($result); | |
echo "</pre>"; | |
} else { | |
echo "Error: ".$response->error; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
session_start(); | |
$state = uniqid(); | |
$_SESSION['state'] = $state; | |
header("Location: https://ssl.reddit.com/api/v1/authorize?scope=identity,comment&state=".$state."&redirect_uri=YOUR REDIRECT URI&response_type=code&client_id=hax"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment