I’m using nextjs14.2 and typescript.
I am creating a music web using Spotify API and currently having a problem with the authentication process.
trying to refresh the token, but the code provided by spotify is node.js and seems to be a bit old version.
I’m trying to handle it using a route handler.
How do I change this code below?
Reference link
app.get('/refresh_token', function(req, res) {
var refresh_token = req.query.refresh_token;
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64'))
},
form: {
grant_type: 'refresh_token',
refresh_token: refresh_token
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token,
refresh_token = body.refresh_token;
res.send({
'access_token': access_token,
'refresh_token': refresh_token
});
}
});
});
I want the code to work without error and get the access_token and refresh_token and save them back to the database.