forked from justinlhudson/SmartThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.js
executable file
·65 lines (51 loc) · 2.21 KB
/
token.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
// http://smartthings.readthedocs.org/en/latest/smartapp-web-services-developers-guide/tutorial-part1.html
// https://community.smartthings.com/t/tutorial-creating-a-rest-smartapp-endpoint
// https://community.smartthings.com/t/oauth-access-token-expiry-and-refresh-token-api/2741
// https://community.smartthings.com/t/amazon-echo-developers-access/10701
// command line (node <app>.js <id> <secret> )
var CLIENT_ID = process.argv[2];
var CLIENT_SECRET = process.argv[3];
var request = require('/usr/local/lib/node_modules/request');
var express = require('/usr/local/lib/node_modules/express'),
app = express();
app.set('port', 3000);
var oauth2 = require('/usr/local/lib/node_modules/simple-oauth2')({
clientID: CLIENT_ID,
clientSecret: CLIENT_SECRET,
site: 'https://graph.api.smartthings.com'
});
// Authorization uri definition
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:'+ app.get('port') +'/callback',
scope: 'app'
});
app.get('/auth', function (req, res) {
res.redirect(authorization_uri);
});
app.get('/', function (req, res) {
res.send('<a href="/auth">Login with SmartThings</a>');
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', function (req, res) {
var code = req.query.code;
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://localhost:'+ app.get('port') +'/callback'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
// result.access_token is the token, get the endpoint
var bearer = result.access_token
var sendreq = { method: "GET", uri: 'https://graph.api.smartthings.com/api/smartapps/endpoints' + "?access_token=" + result.access_token };
request(sendreq, function (err, res1, body) {
var endpoints = JSON.parse(body);
var access_url = endpoints[0].url // last one (not all if any)
console.log(bearer);
console.log(endpoints);
res.send('<pre>https://graph.api.smartthings.com/' + access_url + '</pre><br><pre>Bearer ' + bearer + '</pre>');
});
}
});
app.listen(app.get('port'), function() {
console.log('Node listening on port: ' + app.get('port'))
});