-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3e10a68
Showing
6 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CLIENT_ID= | ||
CLIENT_SECRET= | ||
REDIRECT_URI=http://localhost:8080/auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |