Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

Discord login API wont pass the username PHP

Hello i have this code that gets the authentication token from discord api login but its not giving me the username, this is my code:

<?php
include('api.php');
// Set your app ID and secret
$clientId = '1090332606035394670';
$clientSecret = 'ncY-AgLNNGvkfB73eivKAiKdY9wWPoAj';

// Set the redirect URI for after authorization
$redirectUri = 'http://localhost:81/api/discord.php';

// Start the session
session_start();

// Check if an access token is already set
if (isset($_SESSION['access_token'])) {
    // User is already logged in, so redirect them to your application's home page
	echo  $_SESSION['access_token'];
	die();
    //header('Location: bonushunt.php');
    //exit;
}

// Check if the user has authorized your app
if (isset($_GET['code'])) {
    // Exchange the authorization code for an access token
    $tokenUrl = 'https://discordapp.com/api/oauth2/token';

    $postData = array(
        'grant_type' => 'authorization_code',
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'redirect_uri' => $redirectUri,
        'code' => $_GET['code']
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $tokenUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $response = json_decode($response, true);

    // Store the access token in the session
    $_SESSION['access_token'] = $response['access_token'];
	
	$_SESSION['username'] = $response['username'];
	
	//echo $response['username'];
	print_r($response);
	die();

    // Redirect the user to your application's home page
    header('Location: http://localhost:81/api/bonushunt.php');
    exit;
}

// User is not logged in and has not authorized your app, so redirect them to the Discord authorization page
$authorizationUrl = 'https://discordapp.com/api/oauth2/authorize?' . http_build_query(array(
    'client_id' => $clientId,
    'redirect_uri' => $redirectUri,
    'response_type' => 'code',
    'scope' => 'identify email'
));

header('Location: ' . $authorizationUrl);
exit;
?>
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue with the code is that the username is not returned in the response from the Discord API. Instead, the response only contains an access token. To get the user's username, you need to make an additional API call to the Discord API using the access token.

Here's an example of how to get the user's username using the Discord API:

// Get the user's username using the access token
$userUrl = 'https://discordapp.com/api/users/@me';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $userUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $_SESSION['access_token']
));
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response, true);

// Store the username in the session
$_SESSION['username'] = $response['username'];

You can add this code after the line that stores the access token in the session. This will make an API call to the Discord API using the access token and get the user's username. The username is then stored in the session for later use.

1 like

Please or to participate in this conversation.