Currently I'm developing react, node.js application by integrating spotify api, I want to play music when users visit home page, and play another music when users visit another pages from any specific album(I have album id).
The problem: Since all music should be play start when users visit website, there are no any Authorization (with spotify account login) part in the website.
So, I tried follow Client Crediential Flow in the backend. Here is Node.js backend code to get access token from spotify.
const clientID = process.env.SPOTIFY_CLIENT_ID
const clientSecret = process.env.SPOTIFY_CLIENT_SECRET
const autToken = Buffer.from(`${clientID}:${clientSecret}`, 'utf-8').toString('base64')
const tokenUrl = 'https://accounts.spotify.com/api/token'
const requestBody = qs.stringify({
grant_type: 'client_credentials',
})
const { data }: { data: IAccessTokenResponse } = await axios.post(tokenUrl, requestBody, {
headers: {
Authorization: `Basic ${autToken}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
})
So, now we got access_token with expire duration.
spotify response ===>>> {
access_token: 'BQ.........neU',
token_type: 'Bearer',
expires_in: 3600
}
I'd like to send this token to Front-End to use somehow to play album musics.
But really not sure what should I do in Front-End code to play album musics. By googling, I found react-spotify-web-playback package, it seems I can use access token to play album musics.
<SpotifyPlayer token={access_token from above backend api request}
uris={['spotify:album:ASIFU75iKKE']} />
But it tell me, invalid scope error. My question is
- Where should I put scope to get right access token?
- Current way I'm doing is correct direction to play spotify album music when users visit website?
Any ideas would be welcome.