forked from tbeseda/smartthings-auth-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 2.22 KB
/
index.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
var debug = require('debug')('http');
// Application Configuration
var config = {
port: process.env.PORT || 8080,
api_root: 'https://graph.api.smartthings.com',
server_address: 'YOUR SERVER ADDRESS',
oauth: {
clientID: 'YOUR OAUTH CLIENT ID',
clientSecret: 'YOUR OAUTH CLIENT SECRET',
site: 'https://graph.api.smartthings.com',
tokenPath: '/oauth/token'
}
}
var express = require('express');
var request = require('request');
var oauth2 = require('simple-oauth2')(config.oauth);
// create auth uri for SmartThings
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: config.server_address+'/callback',
scope: 'app'
});
var app = express();
app.get('/', function (req, res) {
res.send('<a href=/auth>Login with SmartThings</a>');
});
app.get('/auth', function (req, res) {
// redirect to SmartThings auth uri
res.redirect(authorization_uri);
});
app.get('/callback', function (req, res) {
// parse request from SmartThings and get access token
var code = req.query.code;
oauth2.authCode.getToken({
code: code,
redirect_uri: config.server_address + '/callback'
}, function (error, result) {
if (error) { debug('Access Token Error', error); }
// extract auth token
var token = oauth2.accessToken.create(result);
// setup request options with uri to get this app's endpoints
// and add retrieved oauth token to auth header
var request_options = {
uri: config.api_root+'/api/smartapps/endpoints',
headers: { Authorization: 'Bearer '+token.token.access_token }
}
request(request_options, function(error, response, body) {
if (error) { debug('Endpoints Request Error', error); }
// extract the app's unique installation url
var installation_url = JSON.parse(body)[0]['url'];
// reuse request options with new uri for the "things" endpoint
// specific to this app installation
request_options.uri = config.api_root + installation_url + '/things'
request(request_options, function(error, response, body){
var all_things = JSON.parse(body)
res.json(all_things); // send JSON of all things
});
});
});
});
app.listen(config.port, function() {
debug('Server running on port', port);
});