Skip to content

Commit 6052492

Browse files
leecalebthelinmichael
authored andcommitted
include auth in get-related-artists example #155
1 parent a84c180 commit 6052492

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

examples/get-related-artists.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,36 @@ var SpotifyWebApi = require('../');
44
* This example shows how to get artists related to another artists. The endpoint is documented here:
55
* https://developer.spotify.com/web-api/get-related-artists/
66
7-
* Please note that this endpoint does not require authentication. However, using an access token
8-
* when making requests will give your application a higher rate limit.
7+
* Please note that authorization is now required and so this example retrieves an access token using the Authorization Code Flow,
8+
* documented here: https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow
99
*/
1010

11-
var spotifyApi = new SpotifyWebApi();
11+
var authorizationCode =
12+
'AQAgjS78s64u1axMCBCRA0cViW_ZDDU0pbgENJ_-WpZr3cEO7V5O-JELcEPU6pGLPp08SfO3dnHmu6XJikKqrU8LX9W6J11NyoaetrXtZFW-Y58UGeV69tuyybcNUS2u6eyup1EgzbTEx4LqrP_eCHsc9xHJ0JUzEhi7xcqzQG70roE4WKM_YrlDZO-e7GDRMqunS9RMoSwF_ov-gOMpvy9OMb7O58nZoc3LSEdEwoZPCLU4N4TTJ-IF6YsQRhQkEOJK';
13+
14+
/* Set the credentials given on Spotify's My Applications page.
15+
* https://developer.spotify.com/my-applications
16+
*/
17+
var spotifyApi = new SpotifyWebApi({
18+
clientId: '<insert client id>',
19+
clientSecret: '<insert client secret>',
20+
redirectUri: '<insert redirect URI>'
21+
});
1222

1323
var artistId = '0qeei9KQnptjwb8MgkqEoy';
1424

15-
spotifyApi.getArtistRelatedArtists(artistId).then(
16-
function(data) {
25+
spotifyApi
26+
.authorizationCodeGrant(authorizationCode)
27+
.then(function(data) {
28+
console.log('Retrieved access token', data.body['access_token']);
29+
30+
// Set the access token
31+
spotifyApi.setAccessToken(data.body['access_token']);
32+
33+
// Use the access token to retrieve information about the user connected to it
34+
return spotifyApi.getArtistRelatedArtists(artistId);
35+
})
36+
.then(function(data) {
1737
if (data.body.artists.length) {
1838
// Print the number of similar artists
1939
console.log('I got ' + data.body.artists.length + ' similar artists!');

0 commit comments

Comments
 (0)