I have written a program that intends to add songs to my own spotify playlist.
The below is the auth code:
const { SpotifyWebApi } = require('spotify-web-api-node');
module.exports = async (req, res) => {
const spotifyApi = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
redirectUri: `${process.env.VERCEL_URL}/api/callback`,
});
const scopes = ['playlist-read-private', 'playlist-read-collaborative', 'playlist-modify-public', 'playlist-modify-private', 'user-read-playback-state', 'user-modify-playback-state', 'user-read-currently-playing', 'playlist-read-collaborative', 'user-read-playback-position', 'user-library-modify'];
const authorizeURL = spotifyApi.createAuthorizeURL(scopes, null);
res.writeHead(302, { Location: authorizeURL });
res.end();
};
While you'd think I'd only need playlist-modify-public and private, I included them all after testing to make sure. The /callback function returns the refresh token which I include in my code.
Yet, when I try await spotifyApi.addTracksToPlaylist(playlistId, [`spotify:track:${songId}`]);, I get the following error:
"An error occurred while communicating with Spotify's Web API.\nDetails: Insufficient client scope."
I don't see how I can have the incorrect client scope?? I've made sure the tokens are correct. I don't know what to do.
The
spotify-web-api-nodenot fully supportAuthorization Code Flow.It means you need to provide a call back endpoint as a local server by express.
And request
codeand exchange intoaccess token.This tutorial code is best example from
spotify-web-api-nodeDemo code
For adding song into your playlist
Save as
demo.jsHow to get your configuration
Install dependencies
Run it
Login by Browser
Again the port number needs to match your configuration.
result
Similar implementation by Next.js
get Spotify Oauth in NextJS but doesn't work