Hi,
I’m working though the final steps of the Jamming React project. I’ve followed the video pretty much line for line at the end. However, I’m getting an error every time I try to search. Spotify redirects me to a blank page with the message ‘INVALID_CLIENT: Invalid client’.
My code looks exactly like the video as far as I can tell. I’ve checked my client ID and redirect Uri and they all match. Using breakpoints in the console on chrome, it looks like the access token is coming back undefined every time I try to search.
Has anyone had this issue before and resolved it?
I’ve put the relevant section of my code below:
let accessToken;
const clientID = 'MY CLIENT ID'
const redirectUri = 'http://localhost:3000'
const Spotify = {
getAccessToken() {
if (accessToken) {
console.log('found an access token')
return accessToken;
}
const accessTokenMatch = window.location.href.match(/access_token=([^&]*)/);
console.log('Access token match: ' + accessTokenMatch)
const expiresInMatch = window.location.href.match(/expires_in=([^&]*)/);
if (accessTokenMatch !== null && expiresInMatch !== null) {
accessToken = accessTokenMatch[1];
const expiresIn = Number(expiresInMatch[1]);
// Clear parameters to get new access token when it expires
window.setTimeout(() => accessToken = "", expiresIn * 1000);
window.history.pushState('Access Token', null, '/');
return accessToken;
}else{
const accessUrl = `https://accounts.spotify.com/authorize?client_id=${clientID}
&response_type=token&scope=playlist-modify-public&redirect_uri=${redirectUri}`
window.location = accessUrl;
}
},
search(term) {
const accessToken = Spotify.getAccessToken();
return fetch(`https://api.spotify.com/v1/search?type=track&q=${term}`,{
headers: {
Authorization: `Bearer ${accessToken}`
}