Ok, we are getting closer.
I use the refresh_token to get the access_token example snippet:
def refreshAccessToken(Locations location) {
String authToken = location.authToken
String refreshToken = location.refreshToken
String clientId = location.client_id
String clientSecret = location.client_secret
// String url = 'https://auth-global.api.smartthings.com/oauth/token'
String url = 'https://api.smartthings.com/oauth/token'
String basicAuth = Base64.getEncoder().encodeToString((clientId + ':' + clientSecret).getBytes(StandardCharsets.UTF_8))
MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>()
params.add('grant_type', 'refresh_token')
params.add('refresh_token', refreshToken)
params.add('client_id', clientId)
params.add('client_secret', clientSecret)
String combine = "Basic "+ basicAuth
def rest = new RestBuilder()
def response = rest.post(url) {
header('Authorization',combine)
accept("application/json")
contentType("application/x-www-form-urlencoded")
body(params)
}
if(response.status == 200) {
location.authToken = response.json.access_token
location.refreshToken = response.json.refresh_token
location.save(failOnError: true, flush: true)
This works well and I don’t see a rate time out of any kind. We have an extensive dashboard and can scan the customer base, selecting a residence and query for behavioral activity, movement history, etc…
I do use subscriptions for device activity , a (motionSensor, contactSensor, waterSensor,temperatureMeasurement, and a few others), one minute scheduler, a 10 minute scheduler, hub health (hub online, offline), device health. The subscriptions are based on capability. It works well and very efficient. Each API smartApp connector (one per Samsung Account) can send their events to us through there own smartApp connector which we install using the developer tools (private workspace). All subscriptions do not require access_tokens.
I really only need the access_token per residence to query the devices for their battery value attribute and to do an action on the remote premise…ie open/close locks or raise/lower a garage door or on/off a switch. So that is why the token is required…
I’ve been trying getting a new authorization code but I keep getting a 302 redirect when I use this snippet to try and get another refresh token:
Am I using the right endpoint? I can’t shake the redirect…
def getAuthCode(Locations location) {
String authToken = location.authToken
String refreshToken = location.refreshToken
String url = ‘SmartThings. Add a little smartness to your things.’
MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>()
params.add(‘response_type’, ‘code’)
params.add(‘client_id’, location.client_id)
params.add(‘scope’,‘app’)
params.add(‘redirect_url’,‘https://…’)
println "fred **GETTING** authcode using client_id: ${location.client_id}"
def rest = new RestBuilder()
def response = rest.post(url) {
accept("application/json")
contentType("application/x-www-form-urlencoded")
body(params)
}