Created
October 10, 2018 16:48
-
-
Save gaycookie/82f05da5cce1c24235e2a627e498cb0a to your computer and use it in GitHub Desktop.
example-oauth2-php
This file contains hidden or 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
| <?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")); | |
| } | |
| ?> |
This file contains hidden or 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
| <?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