first commit
This commit is contained in:
50
utils/getAccessToken.js
Normal file
50
utils/getAccessToken.js
Normal file
@@ -0,0 +1,50 @@
|
||||
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) })
|
||||
|
||||
|
||||
}
|
||||
24
utils/subscribeToEvent.js
Normal file
24
utils/subscribeToEvent.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const axios = require('axios')
|
||||
|
||||
module.exports = async (access_token, session_id, client_id) => {
|
||||
await axios.post('https://api.twitch.tv/helix/eventsub/subscriptions', {
|
||||
type: 'channel.channel_points_custom_reward_redemption.add',
|
||||
version: '1',
|
||||
condition: {
|
||||
broadcaster_user_id: '1337',
|
||||
reward_id: 'abcf127c-7326-4483-a52b-b0da0be61c01'
|
||||
},
|
||||
transport: {
|
||||
method: 'websocket',
|
||||
session_id
|
||||
}
|
||||
}, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${access_token}`,
|
||||
'Client-Id': client_id,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).then(response => {
|
||||
console.log(response.data)
|
||||
}).catch(error => { console.log(error) })
|
||||
}
|
||||
Reference in New Issue
Block a user