Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { request } = require('express');
- const express = require('express');
- const client_id = "id";
- const client_secret = "secret";
- const redirect_uri = "http://localhost:3000/callback";
- const app = express();
- 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('/callback', (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
- };
- }
- });
- app.listen(3000);
Add Comment
Please, Sign In to add comment