-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_oauth.php
61 lines (43 loc) · 2.17 KB
/
example_oauth.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* This example shows how one would authorize a user with Discord via the oAuth2 flow and request their information
* using the token. There are logs to show exactly the information that is generated.
*
* See https://discordapp.com/developers/docs/topics/oauth2
*
* Use this as a guide only. It duplicates code for ease of assessibility.
*
* Author: Lachee
* Last Updated: Feb 2021
* License: MIT
*/
require "discord_oauth.php";
$redirectURL = "http://127.0.0.1/discord/discord-php-kiss/example_oauth.php"; //The URL that the user will be redirect back too
$clientID = "439410995987742720"; //The ID of the client
$clientSecret = file_get_contents("client.key"); //The sensitive and secret key of the client
$scope = "identify email guilds"; //Scoeps you wish to request, seperated by a space
//== STEP 1: Redirect them to the authorize url
//If we do not have a code, then we will redirect them
// The exit; is not required, but its a good explicit practice.
if (!isset($_GET['code'])) {
discord_oauth_redirect($clientID, $scope, $redirectURL, true);
exit;
}
//== STEP 2: Exchange the token
$auth = discord_oauth_exchange($clientID, $clientSecret, $scope, $redirectURL, $_GET['code']);
echo "<h3>Auth Response:</h3>"; var_dump($auth); echo "<hr>";
if ($auth == null || !empty($auth['error']))
die("Failed: Authorization was bad: " . $auth['error']);
//== STEP 3: Get the current user
$user = discord_oauth_get("/users/@me", $auth['access_token']);
echo "<h3>User Response:</h3>"; var_dump($user); echo "<hr>";
if ($user == null || !empty($user['message']))
die("Failed: /users/@me threw a error: " . $user['message']);
//== EXTRA: Get the guilds the user is in
$guilds = discord_oauth_get("/users/@me/guilds", $auth['access_token']);
echo "<h3>Guilds Response:</h3>"; var_dump($guilds); echo "<hr>";
if ($guilds == null || !empty($guilds['message']))
die("Failed: /users/@me/guilds threw a error: " . $guilds['message']);
//== FINALLY: Display the username
echo "Welcome " . $user['username'];
exit;