Skip to content

Instantly share code, notes, and snippets.

@Jengas
Last active February 11, 2025 18:05
Show Gist options
  • Select an option

  • Save Jengas/ad128715cb4f73f5cde9c467edf64b00 to your computer and use it in GitHub Desktop.

Select an option

Save Jengas/ad128715cb4f73f5cde9c467edf64b00 to your computer and use it in GitHub Desktop.

Revisions

  1. Jengas revised this gist Jan 17, 2023. 1 changed file with 0 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -13,18 +13,6 @@
    $apiURLBase = 'https://discord.com/api/users/@me';
    $revokeURL = 'https://discord.com/api/oauth2/token/revoke';

    if(get('action') == 'logout') {
    logout($revokeURL, array(
    'token' => session('access_token'),
    'token_type_hint' => 'access_token',
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    ));
    unset($_SESSION['access_token']);
    header('Location: ' . $_SERVER['PHP_SELF']);
    die();
    }

    session_start();

    // Start the login process by sending the user to Discord's authorization page
  2. Jengas revised this gist Aug 22, 2021. 1 changed file with 35 additions and 8 deletions.
    43 changes: 35 additions & 8 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,19 @@
    $authorizeURL = 'https://discord.com/api/oauth2/authorize';
    $tokenURL = 'https://discord.com/api/oauth2/token';
    $apiURLBase = 'https://discord.com/api/users/@me';
    $revokeURL = 'https://discord.com/api/oauth2/token/revoke';

    if(get('action') == 'logout') {
    logout($revokeURL, array(
    'token' => session('access_token'),
    'token_type_hint' => 'access_token',
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    ));
    unset($_SESSION['access_token']);
    header('Location: ' . $_SERVER['PHP_SELF']);
    die();
    }

    session_start();

    @@ -64,14 +77,15 @@


    if(get('action') == 'logout') {
    // This must to logout you, but it didn't worked(

    $params = array(
    'access_token' => $logout_token
    );

    // Redirect the user to Discord's revoke page
    header('Location: https://discord.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
    // This should logout you
    logout($revokeURL, array(
    'token' => session('access_token'),
    'token_type_hint' => 'access_token',
    'client_id' => OAUTH2_CLIENT_ID,
    'client_secret' => OAUTH2_CLIENT_SECRET,
    ));
    unset($_SESSION['access_token']);
    header('Location: ' . $_SERVER['PHP_SELF']);
    die();
    }

    @@ -97,6 +111,19 @@ function apiRequest($url, $post=FALSE, $headers=array()) {
    return json_decode($response);
    }

    function logout($url, $data=array()) {
    $ch = curl_init($url);
    curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
    CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
    CURLOPT_POSTFIELDS => http_build_query($data),
    ));
    $response = curl_exec($ch);
    return json_decode($response);
    }

    function get($key, $default=NULL) {
    return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
    }
  3. Jengas revised this gist Mar 15, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    );

    // Redirect the user to Discord's authorization page
    header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
    header('Location: https://discord.com/api/oauth2/authorize' . '?' . http_build_query($params));
    die();
    }

    @@ -71,7 +71,7 @@
    );

    // Redirect the user to Discord's revoke page
    header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
    header('Location: https://discord.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
    die();
    }

  4. Jengas revised this gist Jul 7, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@
    define('OAUTH2_CLIENT_ID', '1234567890');
    define('OAUTH2_CLIENT_SECRET', 'verysecretclientcode');

    $authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
    $tokenURL = 'https://discordapp.com/api/oauth2/token';
    $apiURLBase = 'https://discordapp.com/api/users/@me';
    $authorizeURL = 'https://discord.com/api/oauth2/authorize';
    $tokenURL = 'https://discord.com/api/oauth2/token';
    $apiURLBase = 'https://discord.com/api/users/@me';

    session_start();

  5. Jengas created this gist Apr 23, 2018.
    108 changes: 108 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    <?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', '1234567890');
    define('OAUTH2_CLIENT_SECRET', 'verysecretclientcode');

    $authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
    $tokenURL = 'https://discordapp.com/api/oauth2/token';
    $apiURLBase = 'https://discordapp.com/api/users/@me';

    session_start();

    // Start the login process by sending the user to Discord's authorization page
    if(get('action') == 'login') {

    $params = array(
    'client_id' => OAUTH2_CLIENT_ID,
    'redirect_uri' => 'https://yoursite.location/ifyouneedit',
    'response_type' => 'code',
    'scope' => 'identify guilds'
    );

    // Redirect the user to Discord's authorization page
    header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
    die();
    }


    // When Discord redirects the user back here, there will be a "code" and "state" parameter in the query string
    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://yoursite.location/ifyouneedit',
    '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>';

    } else {
    echo '<h3>Not logged in</h3>';
    echo '<p><a href="?action=login">Log In</a></p>';
    }


    if(get('action') == 'logout') {
    // This must to logout you, but it didn't worked(

    $params = array(
    'access_token' => $logout_token
    );

    // Redirect the user to Discord's revoke page
    header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
    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;
    }

    ?>