-
Notifications
You must be signed in to change notification settings - Fork 499
/
get-info-about-current-user.js
54 lines (45 loc) · 1.86 KB
/
get-info-about-current-user.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
const SpotifyWebApi = require('../');
/**
* This example retrieves information about the 'current' user. The current user is the user that has
* authorized the application to access its data.
*/
/* Retrieve a code as documented here:
* https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
*
* Codes are given for a set of scopes. For this example, the scopes are user-read-private and user-read-email.
* Scopes are documented here:
* https://developer.spotify.com/documentation/general/guides/scopes/
*/
const authorizationCode =
'<insert authorization code with user-read-private and user-read-email scopes>';
/* Get the credentials from Spotify's Dashboard page.
* https://developer.spotify.com/dashboard/applications
*/
const spotifyApi = new SpotifyWebApi({
clientId: '<insert client id>',
clientSecret: '<insert client secret>',
redirectUri: '<insert redirect URI>'
});
// First retrieve an access token
spotifyApi
.authorizationCodeGrant(authorizationCode)
.then(function(data) {
console.log('Retrieved access token', data.body['access_token']);
// Set the access token
spotifyApi.setAccessToken(data.body['access_token']);
// Use the access token to retrieve information about the user connected to it
return spotifyApi.getMe();
})
.then(function(data) {
// "Retrieved data for Faruk Sahin"
console.log('Retrieved data for ' + data.body['display_name']);
// "Email is [email protected]"
console.log('Email is ' + data.body.email);
// "Image URL is http://media.giphy.com/media/Aab07O5PYOmQ/giphy.gif"
console.log('Image URL is ' + data.body.images[0].url);
// "This user has a premium account"
console.log('This user has a ' + data.body.product + ' account');
})
.catch(function(err) {
console.log('Something went wrong:', err.message);
});