-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (50 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require('dotenv').config()
const express = require('express')
const app = express()
const port = 8765
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'ejs')
const axios = require('axios')
const api = axios.create({
baseURL: process.env.API_URL
})
const client_id = process.env.CLIENT_ID
const client_secret = process.env.CLIENT_SECRET
const redirect_uri = process.env.REDIRECT_URI
const auth_url = process.env.AUTH_URL
app.get('/', async (req, res) => {
res.render('index.html', {
client_id,
client_secret,
redirect_uri,
auth_url
})
})
app.get('/authorized', async (req, res) => {
console.log(req.query)
const {
token,
state
} = req.query
if(!token || !state) {
res.sendStatus(400)
return
}
const auth = await api.post('/oauth/token', {
authorization_code: token,
client_id,
client_secret
}).catch(err => console.error(err))
console.log(auth)
const podcasts = await api.get('/podcasts', {
headers: {
Authorization: `Bearer ${auth.data.access_token}`
}
}).catch(err => {
console.error(err)
res.send('Something went wrong. Check the console.')
})
console.log(podcasts.data.collection)
res.send(`<head></head><body><h1>Success!</h1><div>Here's your podcast list:</div><div><pre>${JSON.stringify(podcasts.data.collection, 2)}</pre></div></body>`)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))