Skip to content

Commit eac88d8

Browse files
authored
Update authorization examples (#122)
- Add Authorization Code with PKCE extension example - Update Client Credentials example - Add README and package.json files to each folder
1 parent 999766d commit eac88d8

File tree

25 files changed

+2692
-120
lines changed

25 files changed

+2692
-120
lines changed

Diff for: README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## Contents
44

5-
- [Authentication examples](/authentication/)
6-
- [Authorization code]( /authentication/authorization_code/)
7-
- [Client Credentials](/authentication/client_credentials)
8-
- [Implicit Grant](/authentication/implicit_grant/)
5+
- Authorization examples
6+
- [Authorization Code]( /authorization/authorization_code/)
7+
- [Authorization Code with PKCE extension]( /authorization/authorization_code_pkce/)
8+
- [Client Credentials](/authorization/client_credentials)
9+
- [Implicit Grant](/authorization/implicit_grant/)
910
- [Get User Profile example](/get_user_profile/)

Diff for: authentication/README.md

-35
This file was deleted.

Diff for: authentication/client_credentials/app.js

-43
This file was deleted.

Diff for: authentication/package.json

-13
This file was deleted.
File renamed without changes.

Diff for: authorization/authorization_code/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Spotify Authorization Code example
2+
3+
This app displays your Spotify profile information using [Authorization Code](https://developer.spotify.com/documentation/web-api/tutorials/code-flow)
4+
to grant permissions to the app.
5+
6+
## Installation
7+
8+
This example runs on Node.js. On [its website](http://www.nodejs.org/download/) you can find instructions on how to install it.
9+
10+
Install the app dependencies running:
11+
12+
$ npm install
13+
14+
## Using your own credentials
15+
16+
You will need to register your app and get your own credentials from the [Spotify for Developers Dashboard](https://developer.spotify.com/dashboard).
17+
18+
- Create a new app in the dashboard and add `http://localhost:8888/callback` to the app's redirect URL list.
19+
- Once you have created your app, update the `client_id`, `redirect_uri`, and `client_secret` in the `app.js` file with the credentials obtained from the app settings in the dashboard.
20+
21+
## Running the example
22+
23+
From a console shell:
24+
25+
$ npm start
26+
27+
Then, open `http://localhost:8888` in a browser.

Diff for: authentication/authorization_code/app.js renamed to authorization/authorization_code/app.js

+23-24
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,27 @@
44
* the Spotify Accounts.
55
*
66
* For more information, read
7-
* https://developer.spotify.com/web-api/authorization-guide/#authorization_code_flow
7+
* https://developer.spotify.com/documentation/web-api/tutorials/code-flow
88
*/
99

10-
var express = require('express'); // Express web server framework
11-
var request = require('request'); // "Request" library
10+
var express = require('express');
11+
var request = require('request');
12+
var crypto = require('crypto');
1213
var cors = require('cors');
1314
var querystring = require('querystring');
1415
var cookieParser = require('cookie-parser');
1516

16-
var client_id = 'CLIENT_ID'; // Your client id
17-
var client_secret = 'CLIENT_SECRET'; // Your secret
18-
var redirect_uri = 'REDIRECT_URI'; // Your redirect uri
17+
var client_id = 'yourClientIDGoesHere'; // your clientId
18+
var client_secret = 'YourSecretIDGoesHere'; // Your secret
19+
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri
1920

20-
/**
21-
* Generates a random string containing numbers and letters
22-
* @param {number} length The length of the string
23-
* @return {string} The generated string
24-
*/
25-
var generateRandomString = function(length) {
26-
var text = '';
27-
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
2821

29-
for (var i = 0; i < length; i++) {
30-
text += possible.charAt(Math.floor(Math.random() * possible.length));
31-
}
32-
return text;
33-
};
22+
const generateRandomString = (length) => {
23+
return crypto
24+
.randomBytes(60)
25+
.toString('hex')
26+
.slice(0, length);
27+
}
3428

3529
var stateKey = 'spotify_auth_state';
3630

@@ -81,7 +75,8 @@ app.get('/callback', function(req, res) {
8175
grant_type: 'authorization_code'
8276
},
8377
headers: {
84-
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
78+
'content-type': 'application/x-www-form-urlencoded',
79+
Authorization: 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64'))
8580
},
8681
json: true
8782
};
@@ -121,11 +116,13 @@ app.get('/callback', function(req, res) {
121116

122117
app.get('/refresh_token', function(req, res) {
123118

124-
// requesting access token from refresh token
125119
var refresh_token = req.query.refresh_token;
126120
var authOptions = {
127121
url: 'https://accounts.spotify.com/api/token',
128-
headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) },
122+
headers: {
123+
'content-type': 'application/x-www-form-urlencoded',
124+
'Authorization': 'Basic ' + (new Buffer.from(client_id + ':' + client_secret).toString('base64'))
125+
},
129126
form: {
130127
grant_type: 'refresh_token',
131128
refresh_token: refresh_token
@@ -135,9 +132,11 @@ app.get('/refresh_token', function(req, res) {
135132

136133
request.post(authOptions, function(error, response, body) {
137134
if (!error && response.statusCode === 200) {
138-
var access_token = body.access_token;
135+
var access_token = body.access_token,
136+
refresh_token = body.refresh_token;
139137
res.send({
140-
'access_token': access_token
138+
'access_token': access_token,
139+
'refresh_token': refresh_token
141140
});
142141
}
143142
});

0 commit comments

Comments
 (0)