Skip to content

Instantly share code, notes, and snippets.

@nolvuscodes
Created July 10, 2024 05:29
Show Gist options
  • Save nolvuscodes/179a1b5dd7476919656c180222c27598 to your computer and use it in GitHub Desktop.
Save nolvuscodes/179a1b5dd7476919656c180222c27598 to your computer and use it in GitHub Desktop.
Get Your Spotify Token In Console
const SpotifyWebApi = require('spotify-web-api-node');
const express = require('express');
// Replace with your credentials
const clientId = 'YOUR_CLICENT_ID'; // REPLACE YOUR_CLICENT_ID FROM https://developer.spotify.com/dashboard
const clientSecret = 'YOUR_SECRET'; // REPLACE YOUR_SECRET FROM https://developer.spotify.com/dashboard
const redirectUri = 'http://localhost:8888/callback';
// ONCE YOUR DONE, RUN:
// npm install spotify-web-api-node express
// then
// node getSpotifyToken.js
const spotifyApi = new SpotifyWebApi({
clientId: clientId,
clientSecret: clientSecret,
redirectUri: redirectUri
});
const scopes = ['user-read-private', 'user-read-email'];
const authorizeURL = spotifyApi.createAuthorizeURL(scopes);
// Dynamically import the 'open' module
(async () => {
const open = (await import('open')).default;
// Open the authorization URL in the default browser
open(authorizeURL);
})();
// Create an HTTP server to listen for the authorization code
const app = express();
const port = 8888;
app.get('/callback', (req, res) => {
const code = req.query.code;
spotifyApi.authorizationCodeGrant(code).then(
(data) => {
console.log('Access Token:', data.body['access_token']);
console.log('Refresh Token:', data.body['refresh_token']);
res.send('Authorization successful! You can close this tab.');
},
(err) => {
console.error('Authorization error:', err);
res.send('Authorization failed. Check the console for details.');
}
);
});
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment