-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
91 lines (71 loc) · 2.72 KB
/
server.js
File metadata and controls
91 lines (71 loc) · 2.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// server.js
// where your node app starts
// init project
var express = require('express');
var app = express();
// init Spotify API wrapper
var SpotifyWebApi = require('spotify-web-api-node');
var redirectUri = 'https://' + process.env.PROJECT_NAME + '.glitch.me/callback';
var tokenExpirationEpoch;
var spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
redirectUri: redirectUri
});
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.get("/authorize", function (request, response) {
var scopesArray = request.query.scopes.split(',');
var authorizeURL = spotifyApi.createAuthorizeURL(scopesArray);
console.log(authorizeURL)
response.send(authorizeURL);
});
app.get('/getMe', function (req, res) {
spotifyApi.getMe()
.then(function (data) {
res.send(data)
}, function (err) {
console.error(`error`, err);
res.send(err)
});
})
app.get('/playing', function (req, res) {
spotifyApi.getMyCurrentPlaybackState({})
.then(function (data) {
// Output items
console.log("Now Playing: ", data.body);
res.send(data)
}, function (err) {
console.log('Something went wrong!', err);
res.send(err)
});
})
// Exchange Authorization Code for an Access Token
app.get("/callback", function (request, response) {
var authorizationCode = request.query.code;
// Check folks haven't just gone direct to the callback URL
if (!authorizationCode) {
response.redirect('/');
} else {
response.sendFile(__dirname + '/views/callback.html');
}
spotifyApi.authorizationCodeGrant(authorizationCode)
.then(function (data) {
// Set the access token and refresh token
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.setRefreshToken(data.body['refresh_token']);
// Save the amount of seconds until the access token expired
tokenExpirationEpoch = (new Date().getTime() / 1000) + data.body['expires_in'];
console.log('Retrieved token. It expires in ' + Math.floor(tokenExpirationEpoch - new Date().getTime() / 1000) + ' seconds!');
}, function (err) {
console.log('Something went wrong when retrieving the access token!', err.message);
});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});