Skip to content

Instantly share code, notes, and snippets.

@Torbikini
Created March 27, 2019 19:40
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 Torbikini/81bf3d1ec5b64c901ca983325ed00783 to your computer and use it in GitHub Desktop.
Save Torbikini/81bf3d1ec5b64c901ca983325ed00783 to your computer and use it in GitHub Desktop.
example-oauth2-php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_GET["error"])) {
echo json_encode(array("message" => "Authorization Error"));
} elseif (isset($_GET["code"])) {
Header("Location: login.php?code={$_GET["code"]}");
} else {
echo json_encode(array("message" => "No Code Provided"));
}
?>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_GET["error"])) {
echo json_encode(array("message" => "Authorization Error"));
} elseif (isset($_GET["code"])) {
$redirect_uri = "your-request-URI-here";
$token_request = "https://discordapp.com/api/oauth2/token";
$token = curl_init();
curl_setopt_array($token, array(
CURLOPT_URL => $token_request,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
"grant_type" => "authorization_code",
"client_id" => "",
"client_secret" => "",
"redirect_uri" => $redirect_uri,
"code" => $_GET["code"]
)
));
curl_setopt($token, CURLOPT_RETURNTRANSFER, true);
$resp = json_decode(curl_exec($token));
curl_close($token);
if (isset($resp->access_token)) {
$access_token = $resp->access_token;
$info_request = "https://discordapp.com/api/users/@me";
$info = curl_init();
curl_setopt_array($info, array(
CURLOPT_URL => $info_request,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer {$access_token}"
),
CURLOPT_RETURNTRANSFER => true
));
$user = json_decode(curl_exec($info));
curl_close($info);
echo "<h1>Hello, {$user->username}#{$user->discriminator}.</h1><br><h2>{$user->id}</h2><br><img src='https://discordapp.com/api/v6/users/{$user->id}/avatars/{$user->avatar}.jpg' /><br><br>Dashboard Token: {$access_token}";
} else {
echo json_encode(array("message" => "Authentication Error"));
}
} else {
echo json_encode(array("message" => "No Code Provided"));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment