Skip to content

Instantly share code, notes, and snippets.

@Keyinator

Keyinator/index.php

Forked from Jengas/index.php
Last active Nov 10, 2020
Embed
What would you like to do?
Discord oauth2 example PHP
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 300); //300 seconds = 5 minutes. In case if your CURL is slow and is loading too much (Can be IPv6 problem)
error_reporting(E_ALL);
define('OAUTH2_CLIENT_ID', 'PLEASE EDIT');
define('OAUTH2_CLIENT_SECRET', 'PLEASE EDIT');
$authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
$tokenURL = 'https://discordapp.com/api/oauth2/token';
$apiURLBase = 'https://discordapp.com/api/users/@me';
$revokeURL = 'https://discordapp.com/api/oauth2/token/revoke';
session_start();
if(get('action') == 'login') {
$params = array(
'client_id' => OAUTH2_CLIENT_ID,
'redirect_uri' => 'https://dev.keyinator.net/KeyBot/test.php',
'response_type' => 'code',
'scope' => 'identify guilds email'
);
// Redirect the user to Discord's authorization page
header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
die();
}
if(get('code')) {
// Exchange the auth code for a token
$token = apiRequest($tokenURL, array(
"grant_type" => "authorization_code",
'client_id' => OAUTH2_CLIENT_ID,
'client_secret' => OAUTH2_CLIENT_SECRET,
'redirect_uri' => 'https://dev.keyinator.net/KeyBot/test.php',
'code' => get('code')
));
$logout_token = $token->access_token;
$_SESSION['access_token'] = $token->access_token;
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(session('access_token')) {
$user = apiRequest($apiURLBase);
echo '<h3>Logged In</h3>';
echo '<h4>Welcome, ' . $user->username . '</h4>';
echo '<pre>';
print_r($user);
echo '</pre>';
echo '<p><a href="?action=logout">Log Out</a></p>';
} else {
echo '<h3>Not logged in</h3>';
echo '<p><a href="?action=login">Log In</a></p>';
}
if(get('action') == 'logout') {
apiRequest($revokeURL, array(
'token' => session('access_token'),
'client_id' => OAUTH2_CLIENT_ID,
'client_secret' => OAUTH2_CLIENT_SECRET,
));
unset($_SESSION['access_token']);
header('Location: ' . $_SERVER['PHP_SELF']);
die();
}
function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$headers[] = 'Accept: application/json';
if(session('access_token'))
$headers[] = 'Authorization: Bearer ' . session('access_token');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return json_decode($response);
}
function get($key, $default=NULL) {
return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}
function session($key, $default=NULL) {
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}
?>
@Keyinator

This comment has been minimized.

Copy link
Owner Author

@Keyinator Keyinator commented Jun 21, 2019

Fixed logout with the tip of @crusardri

@umair9747

This comment has been minimized.

Copy link

@umair9747 umair9747 commented Jun 25, 2019

So if i implement it on a complete new page, say for example if i have a button on homepage to open another webpage consisting of an upload form, i want the upload page to be only limited to signed in discord user.. So if i just put this whole code in my upload.HTML file will it work? And what's the redirect URL thing in the code?

@OGKingFox

This comment has been minimized.

Copy link

@OGKingFox OGKingFox commented Aug 13, 2019

the API call itself is failing. Only reason this works is because you're removed the access token from session and refreshed the page.

@thegame4craft

This comment has been minimized.

Copy link

@thegame4craft thegame4craft commented Mar 25, 2020

that doesn't work, i will be directed to the main page not in discord to login when i test it.

@Dreven7

This comment has been minimized.

Copy link

@Dreven7 Dreven7 commented Sep 29, 2020

Not working. Thank you =)

@Magicienghost

This comment has been minimized.

Copy link

@Magicienghost Magicienghost commented Nov 10, 2020

It works fine, only problem when I refresh the page while connected, it gives me an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.