Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karljakober committed Nov 27, 2018
0 parents commit 3e10a68
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CLIENT_ID=
CLIENT_SECRET=
REDIRECT_URI=http://localhost:8080/auth
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
17 changes: 17 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Streamlabs API Demo

Set Up:

1) Run `npm install`

2) Register new application on streamlabs.com/dashboard

3) Set callback url to http://localhost:8080/auth

4) Enter credentials into demo .env file

5) Ensure db.sqlite is writable

6) `node index.js`

7) Navigate to http://localhost:8080
Empty file added db.sqlite
Empty file.
57 changes: 57 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require('dotenv').config()

const express = require('express')
const app = express()
const port = 8080
const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('./db.sqlite')
const axios = require('axios')
const STREAMLABS_API_BASE = 'https://www.streamlabs.com/api/v1.0'

app.get('/', (req, res) => {
db.serialize(() => {
db.run("CREATE TABLE IF NOT EXISTS `streamlabs_auth` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `access_token` CHAR(50), `refresh_token` CHAR(50))")

db.get("SELECT * FROM `streamlabs_auth`", (err, row) => {
if (row) {
axios.get(`${STREAMLABS_API_BASE}/donations?access_token=${row.access_token}`).then((response) => {
res.send(`<pre>${JSON.stringify(response.data.data, undefined, 4)}</pre>`)
})
} else {
let authorize_url = `${STREAMLABS_API_BASE}/authorize?`

let params = {
'client_id': process.env.CLIENT_ID,
'redirect_uri': process.env.REDIRECT_URI,
'response_type': 'code',
'scope': 'donations.read+donations.create',
}

// not encoding params
authorize_url += Object.keys(params).map(k => `${k}=${params[k]}`).join('&')

res.send(`<a href="${authorize_url}">Authorize with Streamlabs!</a>`)
}
})
})
})

app.get('/auth', (req, res) => {
let code = req.query.code

axios.post(`${STREAMLABS_API_BASE}/token?`, {
'grant_type': 'authorization_code',
'client_id': process.env.CLIENT_ID,
'client_secret': process.env.CLIENT_SECRET,
'redirect_uri': process.env.REDIRECT_URI,
'code': code
}).then((response) => {
db.run("INSERT INTO `streamlabs_auth` (access_token, refresh_token) VALUES (?,?)", [response.data.access_token, response.data.refresh_token], () => {
res.redirect('/')
})
}).catch((error) => {
console.log(error)
})
})

app.listen(port, () => console.log(`Demo app listening on port ${port}!`))
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "streamlabs-api-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Karl Jakober",
"license": "ISC",
"dependencies": {
"axios": "^0.18.0",
"dotenv": "^6.1.0",
"express": "^4.16.4",
"sqlite3": "^4.0.4"
}
}

0 comments on commit 3e10a68

Please sign in to comment.