50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
const axios = require('axios')
|
|
const express = require('express')
|
|
//const open = require('open')
|
|
const open = (...args) => import('open').then(({default: open}) => open(...args))
|
|
|
|
module.exports = async (client_id, client_secret) => {
|
|
/*
|
|
return await axios.post('https://id.twitch.tv/oauth2/token', {
|
|
client_id,
|
|
client_secret,
|
|
grant_type: 'client_credentials',
|
|
scope: 'channel:manage:redemptions'
|
|
}).then(response => {
|
|
console.log(response.data)
|
|
if (response.data.token_type === 'bearer') return response.data.access_token
|
|
}).catch(error => { console.log(error) })
|
|
*/
|
|
|
|
// Listen on port 3000 for twitch to send us the access token
|
|
const app = express()
|
|
const port = 3000
|
|
|
|
app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
|
|
|
|
// Open the browser to the twitch login page
|
|
await open(`https://id.twitch.tv/oauth2/authorize?client_id=${client_id}&redirect_uri=http://localhost:3000&response_type=code&scope=channel:manage:redemptions`)
|
|
|
|
// Wait for the access token to be sent to us
|
|
let code = await new Promise((resolve, reject) => {
|
|
app.get('/', (req, res) => {
|
|
console.log(req.query)
|
|
res.send('Hello World!')
|
|
resolve(req.query.code)
|
|
})
|
|
})
|
|
|
|
// Use the access token to get the oauth token
|
|
return await axios.post('https://id.twitch.tv/oauth2/token', {
|
|
client_id,
|
|
client_secret,
|
|
code,
|
|
grant_type: 'authorization_code',
|
|
redirect_uri: 'http://localhost:3000'
|
|
}).then(response => {
|
|
console.log(response.data)
|
|
if (response.data.token_type === 'bearer') return response.data.access_token
|
|
}).catch(error => { console.log(error) })
|
|
|
|
|
|
} |