Skip to content

Instantly share code, notes, and snippets.

@themarcusbattle
Last active December 24, 2015 06:49
Show Gist options
  • Save themarcusbattle/6759349 to your computer and use it in GitHub Desktop.
Save themarcusbattle/6759349 to your computer and use it in GitHub Desktop.
Oauth Example
<?php
if ( isset( $_REQUEST['logout'] ) ) {
setcookie("access_token", '', time()-3600);
setcookie("full_name", '', time()-3600);
header('Location: ');
}
if ( isset( $_COOKIE['access_token'] ) ) {
echo "<p>Hey " . $_COOKIE['full_name'] . "! You're already logged in. We don't know anything else about you, but we do know you gave us access to your information lol</p>";
echo "<p><a href=\"?logout=true\">Remove Account</a>";
exit;
}
if ( isset($_REQUEST['code']) ) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://marcus.dev/wp/api/oauth/access_token");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
$data = array(
'client_id' => 'MDI5YjI5YTc3NDQ3OWNmNW',
'client_secret' => 'ExMWQ4OTA1ZmIwNDJlMTBmZWYxYjNhYmU5NWQxYj',
'redirect_uri' => 'http://localhost/oauth.php',
'grant_type' => 'authorization_code',
'code' => $_REQUEST['code']
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp = json_decode(curl_exec($curl));
$info = curl_getinfo($curl);
$error = curl_error($curl);
curl_close($curl);
if ( $error ) {
echo "Error: $error";;
} else {
if ( isset($resp->errors) ) {
foreach ( $resp->errors as $key => $error ) {
$index = $key + 1;
$msg = $error->msg;
echo "Error $index: $msg <br />";
}
}
if ( isset($resp->access_token) ) {
echo "<p>Registration complete!</p>";
setcookie("access_token", $resp->access_token, time()+3600);
setcookie("full_name", $resp->user->full_name, time()+3600);
}
}
}
?>
<a href="http://marcus.dev/wp/api/oauth/authorize?client_id=MDI5YjI5YTc3NDQ3OWNmNW&redirect_uri=http://localhost/oauth.php&response_type=code">NewJC Mobile App Login</a>
<script>
if(window.location.hash) {
alert('You\'re logged in now!');
}
</script>
<?php
if ( isset( $_REQUEST['logout'] ) ) {
setcookie("access_token", '', time()-3600);
setcookie("full_name", '', time()-3600);
header('Location: ');
}
if ( isset( $_COOKIE['access_token'] ) ) {
echo "<p>Hey " . $_COOKIE['full_name'] . "! You're already logged in. We don't know anything else about you, but we do know you gave us access to your information lol</p>";
echo "<p><a href=\"?logout=true\">Remove Account</a>";
exit;
}
if ( isset($_REQUEST['code']) ) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://marcus.dev/wp/api/oauth/access_token");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, true);
$data = array(
'client_id' => 'MDI5YjI5YTc3NDQ3OWNmNW',
'client_secret' => 'ExMWQ4OTA1ZmIwNDJlMTBmZWYxYjNhYmU5NWQxYj',
'redirect_uri' => 'http://localhost/oauth.php',
'grant_type' => 'authorization_code',
'code' => $_REQUEST['code']
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp = json_decode(curl_exec($curl));
$info = curl_getinfo($curl);
$error = curl_error($curl);
curl_close($curl);
if ( $error ) {
echo "Error: $error";;
} else {
if ( isset($resp->errors) ) {
foreach ( $resp->errors as $key => $error ) {
$index = $key + 1;
$msg = $error->msg;
echo "Error $index: $msg <br />";
}
}
if ( isset($resp->access_token) ) {
echo "<p>Registration complete!</p>";
setcookie("access_token", $resp->access_token, time()+3600);
setcookie("full_name", $resp->user->full_name, time()+3600);
}
}
}
?>
<a href="http://marcus.dev/wp/api/oauth/authorize?client_id=MDI5YjI5YTc3NDQ3OWNmNW&redirect_uri=http://localhost/oauth.php&response_type=token">NewJC Mobile App Login</a>
<script>
if(window.location.hash) {
alert('You\'re logged in now!');
}
</script>
<?php
if ( isset($_POST['process']) ) {
}
?>
<form action="http://marcus.dev/wp/api/oauth/access_token" method="POST">
<input type="hidden" name="client_id" value="MDI5YjI5YTc3NDQ3OWNmNW" />
<input type="hidden" name="grant_type" value="password" />
<ul>
<li>
<label>username:</label>
<input type="text" name="username" />
</li>
<li>
<label>passwowrd:</label>
<input type="password" name="password" />
</li>
</ul>
<input type="submit" name="process" value="Log in" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment