PHP code example of teampanfu / oauth2-discord

1. Go to this page and download the library: Download teampanfu/oauth2-discord library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

teampanfu / oauth2-discord example snippets




anfu\OAuth2\Client\Provider\Discord;

session_start();

$provider = new Discord([
    'clientId' => 'YOUR_CLIENT_ID',
    'clientSecret' => 'YOUR_CLIENT_SECRET',
    'redirectUri' => 'http://localhost/callback-url',
]);

if (!empty($_GET['error'])) {
    // Got an error, probably user denied access
    exit('Got error: '.htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8'));
} else if (empty($_GET['code'])) {
    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;
} else if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
    // State is invalid, possible CSRF attack in progress
    unset($_SESSION['oauth2state']);
    exit('Invalid state');
} else {
    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code'],
    ]);

    // Now that you have a token, you can retrieve a user's data
    try {
        $user = $provider->getResourceOwner($token);

        // Depending on which scope you use, you now have access to the user data
        printf('Hello %s#%s!', $user->getUsername(), $user->getDiscriminator());
    } catch (Exception $e) {
        // Failed to get user data
        exit('Something went wrong:'.$e->getMessage());
    }
}

$user = $provider->getResourceOwner($token);

$id = $user->getId();
var_dump($id);
# string(17) "80351110224678912"

$username = $user->getUsername();
var_dump($username);
# string(5) "Nelly"

$discriminator = $user->getDiscriminator();
var_dump($discriminator);
# string(4) "1337"

$avatar = $user->getAvatar();
var_dump($avatar);
# string(32) "8342729096ea3675442027381ff50dfe"

$isBot = $user->getBot();
var_dump($isBot);
# boolean false

$isSystem = $user->getSystem();
var_dump($isSystem);
# boolean false

$isMfaEnabled = $user->getMfaEnabled();
var_dump($isMfaEnabled);
# boolean true

$banner = $user->getBanner();
var_dump($banner);
# string(32) "06c16474723fe537c283b8efa61a30c8"

$accentColor = $user->getAccentColor();
var_dump($accentColor);
# int 16711680

$locale = $user->getLocale();
var_dump($locale);
# string(5) "en-GB"

$verified = $user->getVerified();
var_dump($verified);
# boolean true

$email = $user->getEmail();
var_dump($email);
# string(17) "[email protected]"

$flags = $user->getFlags();
var_dump($flags);
# int 64

$premiumType = $user->getPremiumType();
var_dump($premiumType);
# int 1

$publicFlags = $user->getPublicFlags();
var_dump($publicFlags);
# int 64

$userData = $user->toArray();

$options = [
    'scope' => ['identify', 'email', 'guilds.join'],
];

$authUrl = $provider->getAuthorizationUrl($options);

$provider = new Discord(...);

try {
    $accessToken = $provider->getAccessToken('client_credentials');
} catch (Exception $e) {
    exit('Something went wrong: '.$e->getMessage());
}

$provider = new Discord(...);

$options = [
    'scope' => ['bot'],
    'permissions' => 1,
];

$authUrl = $provider->getAuthorizationUrl($options);

header('Location: '.$authUrl);