[GH-ISSUE #146] Unauthorized #89

Closed
opened 2026-02-27 19:25:59 +03:00 by kerem · 3 comments
Owner

Originally created by @GK-Coding on GitHub (Dec 6, 2018).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/146

I keep getting an error saying the following
Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Unauthorized.
What could be the problem?

Originally created by @GK-Coding on GitHub (Dec 6, 2018). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/146 I keep getting an error saying the following Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Unauthorized. What could be the problem?
kerem closed this issue 2026-02-27 19:25:59 +03:00
Author
Owner

@jwilsson commented on GitHub (Dec 6, 2018):

Hi!
We're gonna need some more to go on. The error indicates your API calls aren't properly authorized, due to a missing access token, an expired access token, etc.

Can you show the code you're using?

<!-- gh-comment-id:444777916 --> @jwilsson commented on GitHub (Dec 6, 2018): Hi! We're gonna need some more to go on. The error indicates your API calls aren't properly authorized, due to a missing access token, an expired access token, etc. Can you show the code you're using?
Author
Owner

@GK-Coding commented on GitHub (Dec 6, 2018):

The auth.php is as follows.
`

<?php

session_start();

require 'backend/dbh.inc.php';
require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'Id',
    'Secret'
);

$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();
$sql = "UPDATE users SET token = '$accessToken', tokentimestamp = CURRENT_TIMESTAMP WHERE id = '$_SESSION[id]'";
mysqli_query($conn, $sql);

// Store the access token somewhere. In a database for example.

// Send the user along and fetch some data!
header('Location: dashboard.php');
die();`

The dashboard file where the api token is set is the following.
`

session_start();
require 'backend/dbh.inc.php';
require 'vendor/autoload.php';

if (!isset($_SESSION['id'])) {
    header("Location: signup.php");
    exit;
}
$sqldeletetoken = "UPDATE users SET token = '' WHERE tokentimestamp < DATE_SUB(NOW(), INTERVAL 50 MINUTE)";
mysqli_query($conn, $sqldeletetoken);
$sqluser = "SELECT * FROM users WHERE id = '$_SESSION[id]'";
$resultuser = mysqli_query($conn, $sqluser);
$rowuser = mysqli_fetch_assoc($resultuser);
$token = $rowuser['token'];

if (!empty($token)) {
    $api = new SpotifyWebAPI\SpotifyWebAPI();
    $api->setAccessToken($token);
} else {
    header("Location: auth.php");
    exit;
}`
<!-- gh-comment-id:444907095 --> @GK-Coding commented on GitHub (Dec 6, 2018): The auth.php is as follows. ` <?php session_start(); require 'backend/dbh.inc.php'; require 'vendor/autoload.php'; $session = new SpotifyWebAPI\Session( 'Id', 'Secret' ); $session->requestCredentialsToken(); $accessToken = $session->getAccessToken(); $sql = "UPDATE users SET token = '$accessToken', tokentimestamp = CURRENT_TIMESTAMP WHERE id = '$_SESSION[id]'"; mysqli_query($conn, $sql); // Store the access token somewhere. In a database for example. // Send the user along and fetch some data! header('Location: dashboard.php'); die();` The dashboard file where the api token is set is the following. ` session_start(); require 'backend/dbh.inc.php'; require 'vendor/autoload.php'; if (!isset($_SESSION['id'])) { header("Location: signup.php"); exit; } $sqldeletetoken = "UPDATE users SET token = '' WHERE tokentimestamp < DATE_SUB(NOW(), INTERVAL 50 MINUTE)"; mysqli_query($conn, $sqldeletetoken); $sqluser = "SELECT * FROM users WHERE id = '$_SESSION[id]'"; $resultuser = mysqli_query($conn, $sqluser); $rowuser = mysqli_fetch_assoc($resultuser); $token = $rowuser['token']; if (!empty($token)) { $api = new SpotifyWebAPI\SpotifyWebAPI(); $api->setAccessToken($token); } else { header("Location: auth.php"); exit; }`
Author
Owner

@GK-Coding commented on GitHub (Dec 6, 2018):

I switched to Authorization Code Flow and it worked

<!-- gh-comment-id:444921212 --> @GK-Coding commented on GitHub (Dec 6, 2018): I switched to Authorization Code Flow and it worked
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#89
No description provided.