Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use axios instead of requests package (deprecated) #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions authorization_code/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -81,16 +81,15 @@ app.get('/callback', function(req, res) {
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
'Authorization': 'Basic ' + (new Buffer.from(`${client_id}:${client_secret}`).toString('base64'))
},
json: true
};

request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {

var access_token = body.access_token,
refresh_token = body.refresh_token;
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;

var options = {
url: 'https://api.spotify.com/v1/me',
Expand All @@ -99,8 +98,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
Expand All @@ -115,6 +114,12 @@ app.get('/callback', function(req, res) {
error: 'invalid_token'
}));
}
}).catch(error => {
console.log(`Error: ${error}`)
res.redirect('/#' +
querystring.stringify({
error: 'invalid_token'
}));
});
}
});
Expand All @@ -125,22 +130,23 @@ 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
},
json: true
};

request.post(authOptions, function(error, response, body) {
if (!error && response.statusCode === 200) {
var access_token = body.access_token;
res.send({
'access_token': access_token
});
}
});
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
});
}
});
});

console.log('Listening on 8888');
Expand Down
16 changes: 8 additions & 8 deletions client_credentials/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,28 +17,27 @@ 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'
},
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: {
'Authorization': 'Bearer ' + token
},
json: true
};
request.get(options, function(error, response, body) {
console.log(body);
axios.get(options.url, options).then(response => {
console.log(response.data);
});
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"express": "~4.16.0",
"cors": "^2.8.4",
"querystring": "~0.2.0",
"request": "~2.83.0"
"axios": "^1.1.3"
}
}