forked from loverajoel/spotify-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
131 lines (116 loc) · 2.95 KB
/
oauth.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Basic examples
* Test a lot of handlers that don't require user login.
*
*/
'use strict'
import Client from '../src/Client';
import UserHandler from '../src/handlers/UserHandler';
import TrackHandler from '../src/handlers/TrackHandler';
import PlaylistHandler from '../src/handlers/PlaylistHandler';
import ArtistHandler from '../src/handlers/ArtistHandler';
let client = Client.instance;
client.settings = {
clientId: 'CLIENT_ID',
secretId: 'SECRET_ID',
scopes: ['user-follow-modify'],
redirect_uri: 'http://localhost:3000/examples/oauth.html'
};
/*
* Login user
* This is a way, you can do it however you want
*/
function session() {
if (sessionStorage.token) {
client.token = sessionStorage.token;
} else if (window.location.hash.split('&')[0].split('=')[1]) {
sessionStorage.token = window.location.hash.split('&')[0].split('=')[1];
client.token = sessionStorage.token;
}
}
session();
function login() {
client.login().then((url) => {
window.location.href = url;
});
}
document.querySelector('#login').onclick = login;
/*
* UserHandler Examples
*
*/
var user = new UserHandler();
/*
* #1 example
* Get the current user.
*/
user.me().then((userEntity) => {
console.log(userEntity);
});
/*
* #2 example
* Get the user by id, should return a User entity.
*/
user.get('1258448899').then((userEntity) => {
console.log(userEntity);
});
/*
* #3 example
* Get the playlists by user id, should return a Playlist collection.
*/
user.playlists('1258448899').then((playlistCollection) => {
console.log(playlistCollection);
});
/*
* Awesome Exmaple
*
* me()
* playlists = me.playlists()
* tracks = playlists[0].tracks();
* artists = tracks[0].artists();
* albums = artists[0].albums();
*/
user.me().then((user) => {
user.playlists().then((playlistCollection) => {
let PlaylistEntity = playlistCollection.first();
PlaylistEntity.tracks.then(tracksCollection => {
let ArtistEntity = tracksCollection[0].artists[0];
ArtistEntity.albums().then(albumsCollection => {
console.log(albumsCollection); // Wooo!!!
});
});
});
});
/*
* Add track to playlist
*
* me()
* playlist = me.playlists()
* track = tracks.search();
* add = playlist.addTrack(track);
* remove = playlist.removeTrack(track);
*/
let myTrack;
new TrackHandler().search('Ginza').then(trackCollection => {
myTrack = trackCollection.first();
});
user.me().then((user) => {
user.playlists('5ViEO6BLk3KN1W6PkkS4TQ').then((playlistEntity) => {
playlistEntity.addTrack([myTrack]).then(() => {
playlistEntity.removeTrack([myTrack]);
});
});
});
/*
* Follow a artist
*/
var artist = new ArtistHandler();
/*
* #4 example
* Get artit with the name 'Muse', follow and unfollow.
*/
artist.search('Muse').then((artistCollection) => {
var muse = artistCollection.first();
muse.follow();
muse.unfollow();
});