-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotifyAPI.js
117 lines (100 loc) · 2.88 KB
/
spotifyAPI.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// this is REALLY bad practice, this should be an environmnet var but this is a hackathon
const CLIENT_ID = '21df40a4eb444b25b9616bbc727d3ecf'
const CLIENT_SECRET = '265599f1a8a142148b7a101b695a317a'
/** git notes
* git add *
* git stash
* git pull
* git stash pop
*/
//using axios
const axios = require('axios').default;
const qs = require('qs');
//https://developer.spotify.com/documentation/general/guides/authorization-guide/
/**this returns a body with json format
{
"access_token": "NgCXRK...MzYjw",
"token_type": "Bearer",
"scope": "user-read-private user-read-email",
"expires_in": 3600,
"refresh_token": "NgAagA...Um_SHo"
}
*/
async function getAuth() {
try {
// const results = await axios({
// method: 'get',
// url: 'https://accounts.spotify.com/authorize',
// query: qs.stringify({
// client_id: CLIENT_ID,
// response_type: 'code',
// redirect_uri: 'https://localhost:3000/',
// })
// })
const results = {client_id: CLIENT_ID,
response_type: 'code',
redirect_uri: 'https://hacklahoma2.herokuapp.com/static/'}
return results;
}
catch (error) {
return error;
}
}
async function getToken() {
try {
const results = await axios({
method: 'post',
url: `https://accounts.spotify.com/api/token`,
data: qs.stringify({
grant_type: "authorization_code",
code: '/authorize endpoint',
redirect_uri: 'https://hacklahoma2.herokuapp.com/static/',
}),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64') // client id and secret from env
}
});
return results;
}
catch (error) {
return error;
}
}
//gets the current user's id, needed for all other calls
async function getCurrentUser(token) {
try {
const results = await axios( {
method: 'get',
url: 'https://api.spotify.com/v1/me',
header: {
'Authorization':'Bearer ' + token,
'Content-Type': 'application/json'
}
});
return results;
}
catch (error) {
return error;
}
}
//creates a new playlist
async function CreatePlaylist(playlistName) {
try {
const results = await axios( {
method: 'post',
url: 'https://api.spotify.com/v1/users/{user_id}/playlists',
body: {
name: playlistName,
}
});
}
catch (error) {
return error;
}
}
async function main() {
let test = await getAuth()
console.log(test);
}
module.exports = { getAuth } ;