parent
3d094c024b
commit
e9d6d4055c
7 changed files with 192 additions and 26 deletions
@ -1,11 +0,0 @@ |
||||
export async function getAuthToken(clientId: string, clientSecret: string) { |
||||
let requestOpts = { |
||||
method: "POST", |
||||
headers: { "Authorization": "Basic " + clientId + ":" + clientSecret }, |
||||
} |
||||
|
||||
const response = await fetch("https://accounts.spotify.com/api/token?grant_type=client_credentials", requestOpts) |
||||
return await response.json(); |
||||
} |
||||
|
||||
export default {} |
@ -0,0 +1,14 @@ |
||||
export async function testSpotify() { |
||||
const requestOpts = { |
||||
method: 'GET', |
||||
}; |
||||
|
||||
const response = await fetch( |
||||
(process.env.REACT_APP_BACKEND || "") + '/spotifycc/v1/search?q=queens&type=artist', |
||||
requestOpts |
||||
); |
||||
if (!response.ok) { |
||||
throw new Error("Response to tag merge not OK: " + JSON.stringify(response)); |
||||
} |
||||
console.log("Spotify response: ", response); |
||||
} |
@ -1,15 +1,65 @@ |
||||
const { createProxyMiddleware } = require('http-proxy-middleware'); |
||||
let axios = require('axios') |
||||
let qs = require('querystring') |
||||
|
||||
// The authorization token to use with the Spotify API.
|
||||
// Will need to be refreshed once in a while.
|
||||
let authToken: string | null = null; |
||||
var authToken: string | null = null; |
||||
|
||||
async function updateToken(clientId: string, clientSecret: string) { |
||||
if (authToken) { return; } |
||||
|
||||
let buf = Buffer.from(clientId + ':' + clientSecret) |
||||
let encoded = buf.toString('base64'); |
||||
|
||||
let response = await axios.post( |
||||
'https://accounts.spotify.com/api/token', |
||||
qs.stringify({ 'grant_type': 'client_credentials' }), |
||||
{ |
||||
'headers': { |
||||
'Authorization': 'Basic ' + encoded, |
||||
'Content-Type': 'application/x-www-form-urlencoded' |
||||
} |
||||
} |
||||
); |
||||
|
||||
export async function getAuthToken(clientId: string, clientSecret: string) { |
||||
let requestOpts = { |
||||
method: "POST", |
||||
headers: { "Authorization": "Basic " + clientId + ":" + clientSecret }, |
||||
authToken = (await response).data.access_token; |
||||
} |
||||
|
||||
const response = await fetch("https://accounts.spotify.com/api/token?grant_type=client_credentials", requestOpts) |
||||
return await response.json(); |
||||
let onProxyReq = (proxyReq: any, req: any, res: any) => { |
||||
proxyReq.setHeader("Authorization", "Bearer " + req._access_token) |
||||
|
||||
console.log("Proxying request", |
||||
{ |
||||
'path': req.path, |
||||
'originalUrl': req.originalUrl, |
||||
'baseUrl': req.baseUrl, |
||||
}, |
||||
{ |
||||
'path': proxyReq.path, |
||||
'originalUrl': proxyReq.originalUrl, |
||||
'baseUrl': req.baseUrl, |
||||
}, |
||||
); |
||||
} |
||||
|
||||
export async function
|
||||
export function useSpotifyClientCreds(app: any) { |
||||
// First add a layer which creates a token and saves it in the request.
|
||||
app.use((req: any, res: any, next: any) => { |
||||
updateToken('c3e5e605e7814cdf94cd86eeba6f4c4f', '5d870c84a3c34aa3a4cf803aa95cb96a') |
||||
.then(() => { |
||||
req._access_token = authToken; |
||||
next(); |
||||
}) |
||||
}) |
||||
app.use( |
||||
'/spotifycc', |
||||
createProxyMiddleware({ |
||||
target: 'https://api.spotify.com/', |
||||
changeOrigin: true, |
||||
onProxyReq: onProxyReq, |
||||
logLevel: 'debug', |
||||
pathRewrite: { '^/spotifycc': '' }, |
||||
}) |
||||
) |
||||
} |
Loading…
Reference in new issue