Last active
November 25, 2020 01:52
-
-
Save nayelyzarazua-bluetrail/73a07f3523ada718765051b91fdba396 to your computer and use it in GitHub Desktop.
OAuth_API_withSubscriptions
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
const RESTrequest = require('request'); | |
/* | |
* Create Subscription Request | |
*/ | |
let subscription = { | |
"sourceType": "CAPABILITY", | |
"capability": { | |
"locationId": "xxxxx-xxxx-xxxx-xxxx", | |
"capability": "switch", | |
"attribute": "switch", | |
"value": "*", | |
"stateChangeOnly": true, | |
"subscriptionName": "switchHandler" | |
} | |
} | |
let createSubs = { | |
method: 'POST', | |
url: `https://api.smartthings.com/v1/installedapps/${appid}/subscriptions`, | |
headers: { | |
Authorization: `Bearer ${access_token}`, | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(subscription) | |
}; | |
RESTrequest(createSubs, function (error, RESTresponse) { | |
if (error){ | |
reject(error); | |
}else{ | |
if (RESTresponse.statusCode != 200) { | |
reject(RESTresponse); | |
} else { | |
resolve(RESTresponse); | |
} | |
} | |
}); | |
/* | |
* SmartApp creation | |
*/ | |
const apiApp = new SmartApp() | |
.enableEventLogging() | |
.appId(app_id)//ApplicationID of the API access app (Workspace) | |
.clientId(client_id) | |
.clientSecret(client_secret) | |
.redirectUri(redirect_uri)//redirectUri, not Target URL | |
.subscribedEventHandler('switchHandler', async (ctx, event) => { | |
console.log(`EVENT ${JSON.stringify(event)}`) | |
console.log(`APP ${ctx.installedAppId}`) | |
}) | |
//Don't forget to add the method that receives the requests from SmartThings (targetURL) | |
router.post('/', async (req, res) => { | |
apiApp.handleHttpCallback(req, res); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment