Skip to content

Instantly share code, notes, and snippets.

@Chriss4123
Last active June 11, 2022 15:43
Show Gist options
  • Save Chriss4123/d4856a36d0978bbd2d90ac3588df162a to your computer and use it in GitHub Desktop.
Save Chriss4123/d4856a36d0978bbd2d90ac3588df162a to your computer and use it in GitHub Desktop.
Discord OAUTH2 With PHP
// I made this OAUTH as the other ones on GitHub weren't working for me. Make sure to replace all the
// necessarry fields with your information.
// To trigger the OAUTH, go to yoursite.com/filenameordir.php?login
// If you want to make the login trigger parameters different, change the if ($_GET['login']) to whatever parameters you want.
// NOTE YOU MUST HAVE cURL installed. If it isn't already (it should be), search it up.
<?php
$urlbuild = array(
'client_id' => "Your Client ID. To retrieve it, go to dc developer portal > OAUTH2 and click Copy under CLIENT ID",
'redirect_uri' => 'Whatever the OAUTH is redirecting to. If you havent configured this already, go into your dev portal, ouath2 and add a new redirect url (required)',
'response_type' => 'code',
'scope' => 'identify guilds' //PUT SCOPES HERE
// THE MOST COMMON SCOPES ARE: identify RETURNS AN OBJECT WITH https://discord.com/developers/docs/resources/user#user-object
// email RETURNS THE EMAIL THEY HAVE REGISTERED THEIR ACCOUNT ON DISCORD WITH
// guilds RETURNS BASIC INFORMATION ABOUT GUILDS https://discord.com/developers/docs/resources/user#get-current-user-guilds
// REMEMBER TO SEPERATE ALL SCOPES WITH A SPACE
);
// When they go to ?login, they will be redirected.
// If you would like to redirect them under different circumstances, change this IF statement.
if (isset($_GET["login"])) {
header('Location: https://discord.com/api/oauth2/authorize' . '?' . http_build_query($urlbuild)); exit();
}
// When they get redirected back to your site, discord automatically does a
// GET request with their OAUTH 2 Code.
if(isset($_GET['code'])) {
// Here we are exchanging the $_GET request with Discords API to get the information.
$ch = curl_init("https://discordapp.com/api/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"grant_type" => "authorization_code",
'client_id' => "Your Client ID. To retrieve it, go to dc developer portal > OAUTH2 and click Copy under CLIENT ID",
'client_secret' => "Your client secret. To retrieve it, go to dc developer portal > OAUTH2 and click Copy under Client Secret",
'redirect_uri' => 'Whatever the OAUTH is redirecting to. If you havent configured this already, go into your dev portal, ouath2 and add a new redirect url (required)',
'code' => $_GET['code']
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlrestemp = curl_exec($ch);
$result = json_decode($curlrestemp);
$access_token = $result->access_token;
$getinfo = curl_init("https://discordapp.com/api/users/@me");
// It is possible to attach this directly, although I like to have it in a variable.
$headers[] = 'Authorization: Bearer ' . $access_token;
// Discord doesn't accept POST requests to retrieve user info, so we have to do this.
curl_setopt($getinfo, CURLOPT_HTTPHEADER, $headers);
curl_setopt($getinfo, CURLOPT_RETURNTRANSFER, 1);
$curlexectemp = curl_exec($getinfo);
$info = json_decode($curlexectemp);
// From here you can get information about the user by doing $info->id for their id, etc.
// If you don't know what fields there are, var_dump $info.
// IF THERE ARE ANY ERRORS, LET ME KNOW!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment