Skip to content

Instantly share code, notes, and snippets.

@nayelyzarazua-bluetrail
Last active November 25, 2020 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayelyzarazua-bluetrail/73a07f3523ada718765051b91fdba396 to your computer and use it in GitHub Desktop.
Save nayelyzarazua-bluetrail/73a07f3523ada718765051b91fdba396 to your computer and use it in GitHub Desktop.
OAuth_API_withSubscriptions
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