From b9c62f983acbd5600b5eceb406a7c378e4b67049 Mon Sep 17 00:00:00 2001 From: dadalmeida1 Date: Thu, 2 Feb 2023 02:10:34 +0100 Subject: [PATCH] working on the API using Auth Code FLow --- Api/package.json | 1 + .../spotify-controller/spotifyCtrl.ts | 50 ++++++++++++++++--- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Api/package.json b/Api/package.json index bfcbf92..f2cf2e8 100644 --- a/Api/package.json +++ b/Api/package.json @@ -29,6 +29,7 @@ "mongodb": "^5.0.0", "mongoose": "^6.9.0", "morgan": "^1.10.0", + "request": "^2.88.2", "swagger-ui-express": "^4.6.0", "winston": "^3.8.2" } diff --git a/Api/src/controller/spotify-controller/spotifyCtrl.ts b/Api/src/controller/spotify-controller/spotifyCtrl.ts index 4fff98f..952f241 100644 --- a/Api/src/controller/spotify-controller/spotifyCtrl.ts +++ b/Api/src/controller/spotify-controller/spotifyCtrl.ts @@ -6,6 +6,9 @@ import axios from 'axios'; import CryptString from './crypt'; import AES from 'crypto-js' import querystring from 'querystring'; +import qs from 'qs'; + +import request from 'request'; class SpotifyController implements Controller { public path = '/spotify'; @@ -197,28 +200,63 @@ class SpotifyController implements Controller { // next(new HttpException(400, 'Cannot create spot')); // } - var code = req.query.code || null; + var code = req.query.code; var state = req.query.state || null; // var storedState = req.cookies ? req.cookies[stateKey] : null; var authOptions = { + method: 'POST', url: 'https://accounts.spotify.com/api/token', - form: { + data: qs.stringify({ code: code, redirect_uri: this.CALLBACK_URL, grant_type: 'authorization_code' - }, + }), headers: { - 'Authorization': 'Basic ' + (new Buffer(this.CLIENT_ID + ':' + this.CLIENT_SECRET).toString('base64')) + 'Authorization': 'Basic ' + ( Buffer.from(this.CLIENT_ID + ':' + this.CLIENT_SECRET).toString('base64')), + 'Content-Type' : 'application/x-www-form-urlencoded' }, json: true }; + try { + console.log('presssquuueee'); + var resp = await axios(authOptions); + if (resp.status === 200) { + console.log(resp); + console.log('oon esttt laaa'); + var access_token = resp.data.access_token; + console.log(access_token); + // should redirect res.redirect('/') + res.redirect('http://localhost:8080/api/ping'); + } + } catch (error) { + next(new HttpException(400, 'On peut pas te connecter mec')); + } + + // axios({ + // method: 'post', + // url: 'https://accounts.spotify.com/api/token', + // data: { + // firstName: 'Fred', + // lastName: 'Flintstone' + // }, + // headers: { + // 'Authorization': 'Basic ' + ( Buffer.from(this.CLIENT_ID + ':' + this.CLIENT_SECRET).toString('base64')), + // 'Content-Type' : 'application/x-www-form-urlencoded' + // }, + // }); + // request.post(authOptions, function(error, response, body) { + // if (!error && response.statusCode === 200) { + // var access_token = body.access_token; + // console.log(access_token); + // res.redirect(200, '/') + // } + // console.log(error); + // }); }; - - private encrypt(text :any){ return CryptoJS.AES.encrypt(text, this.ENCRYPTION_SECRET.stringCrypt).toString(); };