├── .env ├── .gitignore ├── LICENSE ├── README.MD ├── db.sqlite ├── index.js └── package.json /.env: -------------------------------------------------------------------------------- 1 | CLIENT_ID= 2 | CLIENT_SECRET= 3 | REDIRECT_URI=http://localhost:8080/auth 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Streamlabs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # Streamlabs API Demo 2 | 3 | Set Up: 4 | 5 | 1) Run `npm install` 6 | 7 | 2) Register new application on streamlabs.com/dashboard 8 | 9 | 3) Set callback url to http://localhost:8080/auth 10 | 11 | 4) Enter credentials into demo .env file 12 | 13 | 5) Ensure db.sqlite is writable 14 | 15 | 6) `node index.js` 16 | 17 | 7) Navigate to http://localhost:8080 18 | -------------------------------------------------------------------------------- /db.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/streamlabs/streamlabs-api-demo/dd9f36a78db41aba1b1902873ac2ad645bd97e49/db.sqlite -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config() 2 | 3 | const express = require('express') 4 | const app = express() 5 | const port = 8080 6 | const sqlite3 = require('sqlite3').verbose() 7 | const db = new sqlite3.Database('./db.sqlite') 8 | const axios = require('axios') 9 | const STREAMLABS_API_BASE = 'https://www.streamlabs.com/api/v1.0' 10 | 11 | app.get('/', (req, res) => { 12 | db.serialize(() => { 13 | db.run("CREATE TABLE IF NOT EXISTS `streamlabs_auth` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `access_token` CHAR(50), `refresh_token` CHAR(50))") 14 | 15 | db.get("SELECT * FROM `streamlabs_auth`", (err, row) => { 16 | if (row) { 17 | axios.get(`${STREAMLABS_API_BASE}/donations?access_token=${row.access_token}`).then((response) => { 18 | res.send(`
${JSON.stringify(response.data.data, undefined, 4)}
`) 19 | }) 20 | } else { 21 | let authorize_url = `${STREAMLABS_API_BASE}/authorize?` 22 | 23 | let params = { 24 | 'client_id': process.env.CLIENT_ID, 25 | 'redirect_uri': process.env.REDIRECT_URI, 26 | 'response_type': 'code', 27 | 'scope': 'donations.read+donations.create', 28 | } 29 | 30 | // not encoding params 31 | authorize_url += Object.keys(params).map(k => `${k}=${params[k]}`).join('&') 32 | 33 | res.send(`Authorize with Streamlabs!`) 34 | } 35 | }) 36 | }) 37 | }) 38 | 39 | app.get('/auth', (req, res) => { 40 | let code = req.query.code 41 | 42 | axios.post(`${STREAMLABS_API_BASE}/token?`, { 43 | 'grant_type': 'authorization_code', 44 | 'client_id': process.env.CLIENT_ID, 45 | 'client_secret': process.env.CLIENT_SECRET, 46 | 'redirect_uri': process.env.REDIRECT_URI, 47 | 'code': code 48 | }).then((response) => { 49 | db.run("INSERT INTO `streamlabs_auth` (access_token, refresh_token) VALUES (?,?)", [response.data.access_token, response.data.refresh_token], () => { 50 | res.redirect('/') 51 | }) 52 | }).catch((error) => { 53 | console.log(error) 54 | }) 55 | }) 56 | 57 | app.listen(port, () => console.log(`Demo app listening on port ${port}!`)) 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "streamlabs-api-demo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Karl Jakober", 10 | "license": "MIT", 11 | "dependencies": { 12 | "axios": "^0.18.0", 13 | "dotenv": "^6.1.0", 14 | "express": "^4.16.4", 15 | "sqlite3": "^4.0.4" 16 | } 17 | } 18 | --------------------------------------------------------------------------------