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());
}
}