/
test.js
96 lines (82 loc) · 2.53 KB
/
test.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
const router = require("express").Router();
var querystring = require("querystring");
const client_id = "b40371e064584491b035f2a03113cc70";
const client_secret = "f4443f764b544c83b469a9b5b1f76cb8";
var request = require("request");
const axios = require("axios");
router.get("/", (req, res) => {
var scope = "user-read-private user-read-email user-top-read";
res.redirect(
"https://accounts.spotify.com/authorize?" +
querystring.stringify({
response_type: "code",
client_id: "b40371e064584491b035f2a03113cc70",
scope: scope,
redirect_uri: "http://localhost:3001/callback",
})
);
});
router.get("/callback", function (req, res) {
// your application requests refresh and access tokens
// after checking the state parameter
var code = req.query.code || null;
var authOptions = {
url: "https://accounts.spotify.com/api/token",
form: {
code: code,
redirect_uri: "http://localhost:3001/callback",
grant_type: "authorization_code",
},
headers: {
Authorization:
"Basic " +
Buffer.from(client_id + ":" + client_secret).toString("base64"),
},
json: true,
};
request.post(authOptions, async function (error, response, body) {
var profile = {};
if (!error && response.statusCode === 200) {
var access_token = body.access_token,
refresh_token = body.refresh_token;
async function getProfile() {
var options = {
url: "https://api.spotify.com/v1/me",
headers: { Authorization: "Bearer " + access_token },
json: true,
};
await request.get(options, function (error, response, body) {
profile = body;
console.log(profile.id);
});
}
async function getSongs() {
var options = {
url: "https://api.spotify.com/v1/me/top/tracks",
headers: { Authorization: "Bearer " + access_token },
json: true,
};
await request.get(options, function (error, response, body) {
console.log("test2");
goBack();
});
}
async function goBack() {
res.redirect(
"http://localhost:3000/room/BpR8tiltGitkbHgAP4NT?user=" + profile.id
);
}
await getProfile();
await getSongs();
// we can also pass the token to the browser to make requests from there
} else {
res.redirect(
"/#" +
querystring.stringify({
error: "invalid_token",
})
);
}
});
});
module.exports = router;