From f5a4bf843becb462e11bee4357eca65318fccbc7 Mon Sep 17 00:00:00 2001 From: Juan Date: Mon, 24 Jan 2022 16:34:31 +0100 Subject: [PATCH] Exemple API en TD : team spotify --- b3-dev/api/api-td-node/team-spotify/.env | 3 + b3-dev/api/api-td-node/team-spotify/app.js | 96 ++++++++++++++++++++++++ b3-dev/api/api-td-node/team-spotify/package.json | 17 +++++ 3 files changed, 116 insertions(+) create mode 100644 b3-dev/api/api-td-node/team-spotify/.env create mode 100644 b3-dev/api/api-td-node/team-spotify/app.js create mode 100644 b3-dev/api/api-td-node/team-spotify/package.json diff --git a/b3-dev/api/api-td-node/team-spotify/.env b/b3-dev/api/api-td-node/team-spotify/.env new file mode 100644 index 0000000..a318440 --- /dev/null +++ b/b3-dev/api/api-td-node/team-spotify/.env @@ -0,0 +1,3 @@ +SPOTIFY_API_ID=6491f80f28b044beb48e5d9139d98d43 +SPOTIFY_CLIENT_SECRET=3c4b5fdc44314f538a30efa1d74d8771 + diff --git a/b3-dev/api/api-td-node/team-spotify/app.js b/b3-dev/api/api-td-node/team-spotify/app.js new file mode 100644 index 0000000..c9242ae --- /dev/null +++ b/b3-dev/api/api-td-node/team-spotify/app.js @@ -0,0 +1,96 @@ +const axios = require('axios'); +const qs = require('qs'); +require('dotenv').config(); + +const client_id = process.env.SPOTIFY_API_ID; // Your client id +const client_secret = process.env.SPOTIFY_CLIENT_SECRET; // Your secret +const auth_token = Buffer.from(`${client_id}:${client_secret}`, 'utf-8').toString('base64'); + +const getAuth = async () => { + try { + //make post request to SPOTIFY API for access token, sending relavent info + const token_url = 'https://accounts.spotify.com/api/token'; + const data = qs.stringify({ 'grant_type': 'client_credentials' }); + + const response = await axios.post(token_url, data, { + headers: { + 'Authorization': `Basic ${auth_token}`, + 'Content-Type': 'application/x-www-form-urlencoded' + } + }) + //return access token + return response.data.access_token; + //console.log(response.data.access_token); + } catch (error) { + //on fail, log the error in console + console.log(error); + } +} + +const getFirstTrackAlbum = async (album_id) => { + //request token using getAuth() function + const access_token = await getAuth(); + //console.log(access_token); + + const api_url = `https://api.spotify.com/v1/albums/${album_id}/tracks`; + //console.log(api_url); + try { + const response = await axios.get(api_url, { + headers: { + 'Authorization': `Bearer ${access_token}` + } + }) + .then(function (response) { + // handle success + // console.log(response.data); + + let firstTrack = response.data.items[0].name; + console.log("First Track: " + firstTrack); + + return response.data; + + });; + //console.log(response.data); + // return response.data; + } catch (error) { + console.log(error); + } +}; + +const getFirstArtistAlbums = async (artist_id) => { + //request token using getAuth() function + const access_token = await getAuth(); + //console.log(access_token); + + const api_url = `https://api.spotify.com/v1/artists/${artist_id}/albums`; + //console.log(api_url); + try { + const response = await axios.get(api_url, { + headers: { + 'Authorization': `Bearer ${access_token}` + } + }) + .then(function (response) { + // handle success + // console.log(response.data); + let firstAlbum = response.data.items[0].name; + let firstAlbumId = response.data.items[0].id; + + console.log("First Album: " + firstAlbum); + console.log("First Album id: " + firstAlbumId); + + getFirstTrackAlbum(firstAlbumId); + + return response.data; + + }); + // console.log(response.data); + } catch (error) { + console.log(error); + } +}; + +// console.log(getFirstArtistAlbums('0TnOYISbd1XYRBk9myaseg')); +getFirstArtistAlbums('0TnOYISbd1XYRBk9myaseg'); + + diff --git a/b3-dev/api/api-td-node/team-spotify/package.json b/b3-dev/api/api-td-node/team-spotify/package.json new file mode 100644 index 0000000..3d457a5 --- /dev/null +++ b/b3-dev/api/api-td-node/team-spotify/package.json @@ -0,0 +1,17 @@ +{ + "name": "spotify-node", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "axios": "^0.25.0", + "dotenv": "^14.2.0", + "qs": "^6.10.3" + } +}