-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (67 loc) · 2.24 KB
/
Copy pathscript.js
File metadata and controls
77 lines (67 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// NOTE: this Javascript part is required to get an access token. In the future i will switch fully to Rust
// This is according to their tutorial: https://developer.spotify.com/documentation/web-api/tutorials/code-flow
let fs = require('fs');
let client_id = fs.readFileSync('./id.secret', 'utf-8');
let client_secret = fs.readFileSync('./secret.secret', 'utf-8');
let request = require('request');
let express = require('express');
let querystring = require('querystring');
const port = 8888;
let redirect_uri = `http://127.0.0.1:${port}/callback`;
let app = express();
function generateRandomString(length) {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
app.get('/login', function(_req, res) {
let state = generateRandomString(16);
let scope = 'playlist-modify-public playlist-modify-private user-library-read user-library-modify';
res.cookie('spotify_auth_state', state);
res.redirect('https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
client_id: client_id,
scope: scope,
redirect_uri: redirect_uri,
state: state
}));
});
app.get('/callback', function(req, res) {
let code = req.query.code || null;
let state = req.query.state || null;
if (state === null) {
res.redirect('/#' +
querystring.stringify({
error: 'state_mismatch'
}));
} else {
let authOptions = {
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: redirect_uri,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64'))
},
json: true
};
request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
let token = body.access_token;
fs.writeFileSync('./.env', token);
}
else {
console.log('Something went wrong:\n' + error);
}
});
}
});
app.listen(port, () => {
console.log(`Listening at localhost:${port}/login`);
});