From 9796d10d970263d341de80b2a05328a595921185 Mon Sep 17 00:00:00 2001 From: E Bolton Date: Mon, 19 Dec 2022 03:01:04 +0300 Subject: [PATCH 1/4] change from using requests library to axios --- authorization_code/app.js | 54 ++++++++++++++++++++++----------------- package.json | 4 +-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/authorization_code/app.js b/authorization_code/app.js index 9b8a6b55..6fe2f687 100644 --- a/authorization_code/app.js +++ b/authorization_code/app.js @@ -8,7 +8,7 @@ */ var express = require('express'); // Express web server framework -var request = require('request'); // "Request" library +var axios = require('axios'); var cors = require('cors'); var querystring = require('querystring'); var cookieParser = require('cookie-parser'); @@ -73,24 +73,19 @@ app.get('/callback', function(req, res) { })); } else { res.clearCookie(stateKey); - var authOptions = { - url: 'https://accounts.spotify.com/api/token', - form: { - code: code, - redirect_uri: redirect_uri, - grant_type: 'authorization_code' - }, + var authUrl = "https://accounts.spotify.com/api/token"; + var authData = `grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}`; + var authHeaders = { headers: { - 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) - }, - json: true - }; - - request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': 'Basic ' + (Buffer.from(`${client_id}:${client_secret}`).toString('base64')) + } + } - var access_token = body.access_token, - refresh_token = body.refresh_token; + axios.post(authUrl, authData, authHeaders).then(response => { + if (response.status === 200) { + var access_token = response.data.access_token, + refresh_token = response.data.refresh_token; var options = { url: 'https://api.spotify.com/v1/me', @@ -99,8 +94,8 @@ app.get('/callback', function(req, res) { }; // use the access token to access the Spotify Web API - request.get(options, function(error, response, body) { - console.log(body); + axios.get(options.url, options).then(response => { + console.log(response.data); }); // we can also pass the token to the browser to make requests from there @@ -115,6 +110,12 @@ app.get('/callback', function(req, res) { error: 'invalid_token' })); } + }).catch(error => { + console.log(`Error: ${error}`) + res.redirect('/#' + + querystring.stringify({ + error: 'invalid_token' + })); }); } }); @@ -125,7 +126,7 @@ app.get('/refresh_token', function(req, res) { var refresh_token = req.query.refresh_token; var authOptions = { url: 'https://accounts.spotify.com/api/token', - headers: { 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) }, + headers: { 'Authorization': 'Basic ' + (Buffer.from(`${client_id}:${client_secret}`).toString('base64')) }, form: { grant_type: 'refresh_token', refresh_token: refresh_token @@ -133,9 +134,16 @@ app.get('/refresh_token', function(req, res) { json: true }; - request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - var access_token = body.access_token; + axios.post(authOptions.url, + 'grant_type=refresh_token&refresh_token=' + refresh_token, + { + headers: { + 'Authorization': 'Basic ' + (Buffer.from(`${client_id}:${client_secret}`).toString('base64')) + } + } + ).then(response => { + if (response.status === 200) { + var access_token = response.data.access_token; res.send({ 'access_token': access_token }); diff --git a/package.json b/package.json index b40a74ce..c29af2eb 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,10 @@ "description": "Basic examples of the Spotify authorization flows through OAuth 2", "version": "0.0.2", "dependencies": { + "axios": "^1.1.3", "cookie-parser": "1.3.2", "express": "~4.16.0", "cors": "^2.8.4", - "querystring": "~0.2.0", - "request": "~2.83.0" + "querystring": "~0.2.0" } } From 971b37dd27c827fc15806eae656822cdc0920ba6 Mon Sep 17 00:00:00 2001 From: E Bolton Date: Mon, 19 Dec 2022 03:24:34 +0300 Subject: [PATCH 2/4] update client credentials app --- client_credentials/app.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/client_credentials/app.js b/client_credentials/app.js index f6a799af..b7c5d688 100644 --- a/client_credentials/app.js +++ b/client_credentials/app.js @@ -7,7 +7,8 @@ * https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow */ -var request = require('request'); // "Request" library +var axios = require('axios'); +var querystring = require('querystring'); var client_id = 'CLIENT_ID'; // Your client id var client_secret = 'CLIENT_SECRET'; // Your secret @@ -16,7 +17,7 @@ var client_secret = 'CLIENT_SECRET'; // Your secret var authOptions = { url: 'https://accounts.spotify.com/api/token', headers: { - 'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64')) + 'Authorization': 'Basic ' + (new Buffer.from(`${client_id}:${client_secret}`).toString('base64')) }, form: { grant_type: 'client_credentials' @@ -24,11 +25,10 @@ var authOptions = { json: true }; -request.post(authOptions, function(error, response, body) { - if (!error && response.statusCode === 200) { - +axios.post(authOptions.url, querystring.stringify(authOptions.form), authOptions).then(response => { + if (response.status === 200) { // use the access token to access the Spotify Web API - var token = body.access_token; + var token = response.data.access_token; var options = { url: 'https://api.spotify.com/v1/users/jmperezperez', headers: { @@ -36,8 +36,8 @@ request.post(authOptions, function(error, response, body) { }, json: true }; - request.get(options, function(error, response, body) { - console.log(body); + axios.get(options.url, options).then(response => { + console.log(response.data); }); } }); From 704694aea1242c0ee544c370572a5cd95008feaa Mon Sep 17 00:00:00 2001 From: E Bolton Date: Mon, 19 Dec 2022 03:33:08 +0300 Subject: [PATCH 3/4] use querystring for less code change --- authorization_code/app.js | 41 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/authorization_code/app.js b/authorization_code/app.js index 6fe2f687..cfc731cd 100644 --- a/authorization_code/app.js +++ b/authorization_code/app.js @@ -73,16 +73,21 @@ app.get('/callback', function(req, res) { })); } else { res.clearCookie(stateKey); - var authUrl = "https://accounts.spotify.com/api/token"; - var authData = `grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}`; - var authHeaders = { + var authOptions = { + url: 'https://accounts.spotify.com/api/token', + form: { + code: code, + redirect_uri: redirect_uri, + grant_type: 'authorization_code' + }, headers: { 'Content-Type': 'application/x-www-form-urlencoded', - 'Authorization': 'Basic ' + (Buffer.from(`${client_id}:${client_secret}`).toString('base64')) - } - } + 'Authorization': 'Basic ' + (new Buffer.from(`${client_id}:${client_secret}`).toString('base64')) + }, + json: true + }; - axios.post(authUrl, authData, authHeaders).then(response => { + axios.post(authOptions.url, querystring.stringify(authOptions.form), authOptions).then(response => { if (response.status === 200) { var access_token = response.data.access_token, refresh_token = response.data.refresh_token; @@ -134,21 +139,15 @@ app.get('/refresh_token', function(req, res) { json: true }; - axios.post(authOptions.url, - 'grant_type=refresh_token&refresh_token=' + refresh_token, - { - headers: { - 'Authorization': 'Basic ' + (Buffer.from(`${client_id}:${client_secret}`).toString('base64')) + axios.post(authOptions.url, querystring.stringify(authOptions.form), authOptions) + .then(response => { + if (response.status === 200) { + var access_token = response.data.access_token; + res.send({ + 'access_token': access_token + }); } - } - ).then(response => { - if (response.status === 200) { - var access_token = response.data.access_token; - res.send({ - 'access_token': access_token - }); - } - }); + }); }); console.log('Listening on 8888'); From fb7c2cccd190d146420643ddf66ca9d3bfbcd802 Mon Sep 17 00:00:00 2001 From: E Bolton Date: Mon, 19 Dec 2022 03:45:47 +0300 Subject: [PATCH 4/4] small cleanup --- authorization_code/app.js | 1 - package.json | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/authorization_code/app.js b/authorization_code/app.js index cfc731cd..a88a8f68 100644 --- a/authorization_code/app.js +++ b/authorization_code/app.js @@ -81,7 +81,6 @@ app.get('/callback', function(req, res) { grant_type: 'authorization_code' }, headers: { - 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'Basic ' + (new Buffer.from(`${client_id}:${client_secret}`).toString('base64')) }, json: true diff --git a/package.json b/package.json index c29af2eb..d235f632 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,10 @@ "description": "Basic examples of the Spotify authorization flows through OAuth 2", "version": "0.0.2", "dependencies": { - "axios": "^1.1.3", "cookie-parser": "1.3.2", "express": "~4.16.0", "cors": "^2.8.4", - "querystring": "~0.2.0" + "querystring": "~0.2.0", + "axios": "^1.1.3" } }