I'm trying to access the Spotify access_token from Postman, but it doesn't seem to be going through. When I try to submit a GET request on Postman for http://localhost:3001/access, I just get "server running". Also, keeping the redirect request in '/login' in mind, when calling a get request from the front end, would I call a request for http://localhost:3001/access or http://localhost:3001/login?
server.js
const app = express();
app.use(cors());
app.set('port', 3001);
app.get('/login', (req, res) => {
const scope = 'user-read-private user-read-email';
const state = Math.random().toString(36).slice(2,18);
const auth_query_parameters = new URLSearchParams({
response_type: "code",
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
});
res.redirect('http://accounts.spotify.com/authorize?' +
auth_query_parameters.toString());
});
app.get('/access', (req, res) => {
const code = req.query.code || null;
const state = req.query.state || null;
const state_mismatch = new URLSearchParams({
error: 'state_mismatch'
});
if (state == null) {
res.redirect('/#' +
state_mismatch.toString());
}
else {
const authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (Buffer.from(client_id + ':' + client_secret).toString('base64')),
'Content-Type': "application/x-www-form-urlencoded"
},
json: true
};
request.post(authOptions, (error, response, body) => {
if (!error && response.statusCode === 200) {
const access_token = body.access_token;
res.send({
'access_token': access_token
});
}
});
}
res.send('hello');
})
app.get('/', (req, res) => {
res.send('server running');
})
app.listen(3001);