Announcements
The Spotify Community is in read-only mode while we move to a new platform. We expect to be back up and running October 6th. During this time you can read existing content but take no other actions. If you still need help, visit our Support Site or contact us.

Help Wizard

Step 1

NEXT STEP

API blocked for Premium users?

API blocked for Premium users?

Plan

Free

Country

 

My Question or Issue

I am developing a first app on Spotify using Authorization if there is no token. The app generates a random song, and tries again if it's not able to load it.
It works for my Spotufy account which is free, but with a friend's Premium account, it just tries loading songs over and over again with a 403 error.

Here is my script.js code:

// Get the hash of the url
const hash = window.location.hash
.substring(1)
.split("&")
.reduce(function(initial, item) {
if (item) {
var parts = item.split("=");
initial[parts[0]] = decodeURIComponent(parts[1]);
}
return initial;
}, {});
window.location.hash = "";

// Set token
let _token = hash.access_token;

const authEndpoint = "https://accounts.spotify.com/authorize";

// Replace with your app's client ID, redirect URI and desired scopes
const clientId = "";
const redirectUri = "http://127.0.0.1:5000";
const scopes = [
"streaming",
"user-modify-playback-state",
"user-library-modify"
];

// If there is no token, redirect to Spotify authorization
if (!_token) {
window.location = `${authEndpoint}?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scopes.join(
"%20"
)}&response_type=token`;
}

// Set up the Web Playback SDK
let deviceId;
let ids = [];

window.onSpotifyPlayerAPIReady = () => {
const player = new Spotify.Player({
name: "Big Spotify Button",
getOAuthToken: cb => {
cb(_token);
}
});

// Error handling
player.on("initialization_error", e => console.error(e));
player.on("authentication_error", e => console.error(e));
player.on("account_error", e => console.error(e));
player.on("playback_error", e => console.error(e));

// Playback status updates
player.on("player_state_changed", state => {
// console.log(state);
});

// Ready
player.on("ready", data => {
// console.log("Ready with Device ID", data.device_id);
deviceId = data.device_id;
});

// Connect to the player!
player.connect();
};

// Play a specified track on the Web Playback SDK's device ID
function play(device_id, track) {
$.ajax({
url: "https://api.spotify.com/v1/me/player/play?device_id=" + device_id,
type: "PUT",
data: `{"uris": ["${track}"]}`,
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + _token);
},
success: function(data) {
}
});
}

function makeid(length) {
var result = "";
var characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

function restart() {
//Reload page
location.reload();
}

function getASong() {
let random_seed = makeid(2);
let random_offset = Math.floor(Math.random() * 2000); // returns a random integer from 0 to 9
$.ajax({
url:
"https://api.spotify.com/v1/search?type=track&offset=" +
random_offset +
"&limit=1&q=" +
random_seed,
type: "GET",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + _token);
},
success: function(data) {
let trackUri = data.tracks.items[0].uri;
let releaseDate = data.tracks.items[0].album.release_date;

play(deviceId, trackUri);
$("#current-track-name-save").attr("data-song", data.tracks.items[0].uri);
$("#current-track-name-save").attr(
"src",
"https://cdn.glitch.com/eed3cfeb-d097-4769-9d03-2d3a6cc7c004%2Ficons8-heart-24.png?v=1597232027543"
);
$("#embed-uri").attr(
"src",
"https://open.spotify.com/embed/track/" + data.tracks.items[0].id
);
$("#current-track-name-save").css("display", "block");

$(".start").hide();
},
error : function() {
getASong();
}
});
}
Reply
1 Reply

I think it has nothing to do with your friend having premium, but I think your app is still in developer mode. You can read about it here: https://developer.spotify.com/documentation/web-api/concepts/quota-modes

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 5 years ago  in Social & Random