-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (90 loc) · 2.9 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const express = require('express')
const bodyParser = require('body-parser')
const expressHandlebars = require('express-handlebars')
const Ustream = require('ustream-sdk')
const app = express()
app.engine('handlebars', expressHandlebars())
app.set('view engine', 'handlebars')
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: false }))
/**
* The app's entry point.
*
* Renders two buttons which link to Ustream's oauth login page. This
* application assumes your USTREAM_REDIRECT_URI returns to:
*
* https://yourDomain.com/channels
*/
app.get('/', (req, res) => {
const oauthParams = {
client_id: process.env.USTREAM_CLIENT_ID,
redirect_uri: process.env.USTREAM_REDIRECT_URI,
token_type: "bearer"
}
// Append oauthParams as query string parameters to the authorization url.
const implicit_flow_endpoint = Object.keys(oauthParams)
.reduce((queryString, param) => {
return `${queryString}&${param}=${oauthParams[param]}`
}, `https://www.ustream.tv/oauth2/authorize?response_type=token`)
const auth_code_flow_endpoint = Object.keys(oauthParams)
.reduce((queryString, param) => {
return `${queryString}&${param}=${oauthParams[param]}`
}, `https://www.ustream.tv/oauth2/authorize?response_type=code`)
res.render('index', {
implicit_flow_endpoint,
auth_code_flow_endpoint
})
})
/**
* The endpoint where your OAuth redirect should send the user after
* they have logged in to their Ustream account.
*
* http://developers.video.ibm.com/channel-api/getting-started.html#authorization-flows_2
*/
app.get('/channels', (req, res) => {
let ustream = null
if (req.query.code) {
// Authorization code flow
ustream = new Ustream({
type: 'oauth_code',
client_id: process.env.USTREAM_CLIENT_ID,
client_secret: process.env.USTREAM_CLIENT_SECRET,
code: req.query.code,
redirect_uri: process.env.USTREAM_REDIRECT_URI
})
} else if (req.query.access_token) {
// Implicit flow
ustream = new Ustream({
type: 'oauth_token',
access_token: req.query.access_token,
token_type: req.query.token_type,
expires_in: req.query.expires_in
})
} else {
// No code or token provided. Show error.
res.render('channels', {
success: false
})
return null
}
ustream.channel.list()
.then((channels) => {
// Ustream's API returns a page of channels as an object, where the keys
// are the ids of each channel and the values are their corresponding
// fields. This extracts the data for each channel as an array.
const channelData = Object.keys(channels.data).map((id) => {
return channels.data[id]
})
res.render('channels', {
success: true,
channels: channelData
})
})
.catch(() => {
// Oauth failed. Show error.
res.render('channels', {
success: false
})
})
})
app.listen(3000)