forked from FriedSu/musicart-issp-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.js
109 lines (86 loc) · 2.84 KB
/
tokens.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var SpotifyWebApi = require('spotify-web-api-node');
const express = require('express')
const scopes = [
'ugc-image-upload',
'user-read-playback-state',
'user-modify-playback-state',
'user-read-currently-playing',
'streaming',
'app-remote-control',
'user-read-email',
'user-read-private',
'playlist-read-collaborative',
'playlist-modify-public',
'playlist-read-private',
'playlist-modify-private',
'user-library-modify',
'user-library-read',
'user-top-read',
'user-read-playback-position',
'user-read-recently-played',
'user-follow-read',
'user-follow-modify'
];
var spotifyApi = new SpotifyWebApi({
clientId: '475065ad09b14f60a13da67ff08de3f0',
clientSecret: 'c86ce757d0094f08883821a2d57444bd',
redirectUri: 'http://localhost:8090/callback'
});
const app = express();
app.get('/login', (req, res) => {
res.redirect(spotifyApi.createAuthorizeURL(scopes));
});
app.get('/callback', (req, res) => {
const error = req.query.error;
const code = req.query.code;
const state = req.query.state;
if (error) {
console.error('Callback Error:', error);
res.send(`Callback Error: ${error}`);
return;
}
spotifyApi
.authorizationCodeGrant(code)
.then(data => {
const access_token = data.body['access_token'];
const refresh_token = data.body['refresh_token'];
const expires_in = data.body['expires_in'];
spotifyApi.setAccessToken(access_token);
spotifyApi.setRefreshToken(refresh_token);
console.log('access_token:', access_token);
console.log('refresh_token:', refresh_token);
const fs = require('fs')
fs.writeFile('token.txt', access_token, err => {
if (err) {
console.error(err)
return
}
})
console.log(
`Sucessfully retreived access token. Expires in ${expires_in} s.`
);
res.send('Success! You can now close the window.');
setInterval(async () => {
const data = await spotifyApi.refreshAccessToken();
const access_token = data.body['access_token'];
console.log('The access token has been refreshed!');
console.log('access_token:', access_token);
spotifyApi.setAccessToken(access_token);
fs.writeFile('token.txt', access_token, err => {
if (err) {
console.error(err)
return
}
})
}, expires_in / 2 * 1000);
})
.catch(error => {
console.error('Error getting Tokens:', error);
res.send(`Error getting Tokens: ${error}`);
});
});
app.listen(8090, () =>
console.log(
'HTTP Server up. Now go to http://localhost:8090/login in your browser.'
)
);