This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
forked from Xwilarg/Discord-OAuth2-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
47 lines (42 loc) · 2.15 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require __DIR__ . '/vendor/autoload.php';
use Xwilarg\Discord\OAuth2;
// CLIENT-ID-HERE: Replace this with the Client ID of the application
// SECRET: Replace this with the Secret of the application
// CALLBACK-URL: Replace this with the redirect URL (URL called after the user is logged in, must be registered in https://discordapp.com/developers/applications/[YourAppId]/oauth)
// Enter your Discord Oauth details here:
$oauth2 = new OAuth2("CLIENT-ID-HERE", "SECRET", "CALLBACK-URL");
if ($oauth2->isRedirected() === false) { // Did the client already logged in ?
// The parameters can be a combination of the following: connections, email, identity or guilds
// More information about it here: https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
// The others parameters are not available with this library
$oauth2->startRedirection(['identify', 'connections']);
} else {
// We preload the token to see if everything happened without error
$ok = $oauth2->loadToken();
if ($ok !== true) {
// A common error can be to reload the page because the code returned by Discord would still be present in the URL
// If this happen, isRedirected will return true and we will come here with an invalid code
// So if there is a problem, we redirect the user to Discord authentification
$oauth2->startRedirection(['identify', 'connections']);
} else {
// ---------- USER INFORMATION
$answer = $oauth2->getUserInformation(); // Same as $oauth2->getCustomInformation('users/@me')
if (array_key_exists("code", $answer)) {
exit("An error occured: " . $answer["message"]);
} else {
echo "Welcome " . $answer["username"] . "#" . $answer["discriminator"];
}
echo '<br/><br/>';
// ---------- CONNECTIONS INFORMATION
$answer = $oauth2->getConnectionsInformation();
if (array_key_exists("code", $answer)) {
exit("An error occured: " . $answer["message"]);
} else {
foreach ($answer as $a) {
echo $a["type"] . ': ' . $a["name"] . '<br/>';
}
}
}
}
?>