mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-08 05:31:56 +00:00
Jawbone Global Oauth
This commit is contained in:
@@ -17,7 +17,6 @@ definition(
|
||||
) {
|
||||
appSetting "clientId"
|
||||
appSetting "clientSecret"
|
||||
appSetting "serverUrl"
|
||||
}
|
||||
|
||||
preferences {
|
||||
@@ -28,16 +27,16 @@ mappings {
|
||||
path("/receivedToken") { action: [ POST: "receivedToken", GET: "receivedToken"] }
|
||||
path("/receiveToken") { action: [ POST: "receiveToken", GET: "receiveToken"] }
|
||||
path("/hookCallback") { action: [ POST: "hookEventHandler", GET: "hookEventHandler"] }
|
||||
path("/oauth/initialize") {action: [GET: "oauthInitUrl"]}
|
||||
path("/oauth/callback") { action: [ GET: "callback" ] }
|
||||
}
|
||||
|
||||
def getSmartThingsClientId() {
|
||||
return appSettings.clientId
|
||||
}
|
||||
|
||||
def getSmartThingsClientSecret() {
|
||||
return appSettings.clientSecret
|
||||
}
|
||||
def getSmartThingsClientId() { return appSettings.clientId }
|
||||
def getSmartThingsClientSecret() { return appSettings.clientSecret }
|
||||
def getServerUrl() { return "https://graph.api.smartthings.com" }
|
||||
def getShardUrl() { return getApiServerUrl() }
|
||||
def getBuildRedirectUrl() { "${serverUrl}/oauth/initialize?appId=${app.id}&access_token=${state.accessToken}&apiServerUrl=${shardUrl}" }
|
||||
def buildRedirectUrl(page) { return buildActionUrl(page) }
|
||||
|
||||
def callback() {
|
||||
def redirectUrl = null
|
||||
@@ -47,7 +46,7 @@ def callback() {
|
||||
} else {
|
||||
log.warn "No authQueryString"
|
||||
}
|
||||
|
||||
|
||||
if (state.JawboneAccessToken) {
|
||||
log.debug "Access token already exists"
|
||||
setup()
|
||||
@@ -77,20 +76,21 @@ def callback() {
|
||||
|
||||
def authPage() {
|
||||
log.debug "authPage"
|
||||
def description = null
|
||||
def description = null
|
||||
if (state.JawboneAccessToken == null) {
|
||||
if (!state.accessToken) {
|
||||
log.debug "About to create access token"
|
||||
createAccessToken()
|
||||
}
|
||||
description = "Click to enter Jawbone Credentials"
|
||||
def redirectUrl = oauthInitUrl()
|
||||
// log.debug "RedirectURL = ${redirectUrl}"
|
||||
return dynamicPage(name: "Credentials", title: "Jawbone UP", nextPage: null, uninstall: true, install:false) {
|
||||
section { href url:redirectUrl, style:"embedded", required:true, title:"Jawbone UP", description:description }
|
||||
def redirectUrl = buildRedirectUrl
|
||||
log.debug "RedirectURL = ${redirectUrl}"
|
||||
def donebutton= state.JawboneAccessToken != null
|
||||
return dynamicPage(name: "Credentials", title: "Jawbone UP", nextPage: null, uninstall: true, install: donebutton) {
|
||||
section { href url:redirectUrl, style:"embedded", required:true, title:"Jawbone UP", state: hast ,description:description }
|
||||
}
|
||||
} else {
|
||||
description = "Jawbone Credentials Already Entered."
|
||||
description = "Jawbone Credentials Already Entered."
|
||||
return dynamicPage(name: "Credentials", title: "Jawbone UP", uninstall: true, install:true) {
|
||||
section { href url: buildRedirectUrl("receivedToken"), style:"embedded", state: "complete", title:"Jawbone UP", description:description }
|
||||
}
|
||||
@@ -101,8 +101,8 @@ def oauthInitUrl() {
|
||||
log.debug "oauthInitUrl"
|
||||
def stcid = getSmartThingsClientId()
|
||||
state.oauthInitState = UUID.randomUUID().toString()
|
||||
def oauthParams = [ response_type: "code", client_id: stcid, scope: "move_read sleep_read", redirect_uri: buildRedirectUrl("receiveToken") ]
|
||||
return "https://jawbone.com/auth/oauth2/auth?${toQueryString(oauthParams)}"
|
||||
def oauthParams = [ response_type: "code", client_id: stcid, scope: "move_read sleep_read", redirect_uri: "${serverUrl}/oauth/callback" ]
|
||||
redirect(location: "https://jawbone.com/auth/oauth2/auth?${toQueryString(oauthParams)}")
|
||||
}
|
||||
|
||||
def receiveToken(redirectUrl = null) {
|
||||
@@ -113,7 +113,7 @@ def receiveToken(redirectUrl = null) {
|
||||
def params = [
|
||||
uri: "https://jawbone.com/auth/oauth2/token?${toQueryString(oauthParams)}",
|
||||
]
|
||||
httpGet(params) { response ->
|
||||
httpGet(params) { response ->
|
||||
log.debug "${response.data}"
|
||||
log.debug "Setting access token to ${response.data.access_token}, refresh token to ${response.data.refresh_token}"
|
||||
state.JawboneAccessToken = response.data.access_token
|
||||
@@ -155,7 +155,7 @@ def connectionStatus(message, redirectUrl = null) {
|
||||
<meta http-equiv="refresh" content="3; url=${redirectUrl}" />
|
||||
"""
|
||||
}
|
||||
|
||||
|
||||
def html = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -231,19 +231,11 @@ String toQueryString(Map m) {
|
||||
return m.collect { k, v -> "${k}=${URLEncoder.encode(v.toString())}" }.sort().join("&")
|
||||
}
|
||||
|
||||
def getServerUrl() { return appSettings.serverUrl ?: "https://graph.api.smartthings.com" }
|
||||
|
||||
def buildRedirectUrl(page) {
|
||||
// log.debug "buildRedirectUrl"
|
||||
// /api/token/:st_token/smartapps/installations/:id/something
|
||||
return "${serverUrl}/api/token/${state.accessToken}/smartapps/installations/${app.id}/${page}"
|
||||
}
|
||||
|
||||
def validateCurrentToken() {
|
||||
log.debug "validateCurrentToken"
|
||||
def url = "https://jawbone.com/nudge/api/v.1.1/users/@me/refreshToken"
|
||||
def requestBody = "secret=${getSmartThingsClientSecret()}"
|
||||
|
||||
|
||||
try {
|
||||
httpPost(uri: url, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ], body: requestBody) {response ->
|
||||
if (response.status == 200) {
|
||||
@@ -287,9 +279,10 @@ def validateCurrentToken() {
|
||||
}
|
||||
|
||||
def initialize() {
|
||||
def hookUrl = "${serverUrl}/api/token/${state.accessToken}/smartapps/installations/${app.id}/hookCallback"
|
||||
log.debug "Callback URL - Webhook"
|
||||
def localServerUrl = getApiServerUrl()
|
||||
def hookUrl = "${localServerUrl}/api/token/${state.accessToken}/smartapps/installations/${app.id}/hookCallback"
|
||||
def webhook = "https://jawbone.com/nudge/api/v.1.1/users/@me/pubsub?webhook=$hookUrl"
|
||||
log.debug "Callback URL: $webhook"
|
||||
httpPost(uri: webhook, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ])
|
||||
}
|
||||
|
||||
@@ -299,16 +292,16 @@ def setup() {
|
||||
|
||||
if (state.JawboneAccessToken) {
|
||||
def urlmember = "https://jawbone.com/nudge/api/users/@me/"
|
||||
def member = null
|
||||
httpGet(uri: urlmember, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
def member = null
|
||||
httpGet(uri: urlmember, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
member = response.data.data
|
||||
}
|
||||
|
||||
|
||||
if (member) {
|
||||
state.member = member
|
||||
def externalId = "${app.id}.${member.xid}"
|
||||
|
||||
// find the appropriate child device based on my app id and the device network id
|
||||
// find the appropriate child device based on my app id and the device network id
|
||||
def deviceWrapper = getChildDevice("${externalId}")
|
||||
|
||||
// invoke the generatePresenceEvent method on the child device
|
||||
@@ -328,7 +321,7 @@ def setup() {
|
||||
|
||||
def installed() {
|
||||
enableCallback()
|
||||
|
||||
|
||||
if (!state.accessToken) {
|
||||
log.debug "About to create access token"
|
||||
createAccessToken()
|
||||
@@ -341,7 +334,7 @@ def installed() {
|
||||
|
||||
def updated() {
|
||||
enableCallback()
|
||||
|
||||
|
||||
if (!state.accessToken) {
|
||||
log.debug "About to create access token"
|
||||
createAccessToken()
|
||||
@@ -365,29 +358,29 @@ def uninstalled() {
|
||||
}
|
||||
|
||||
def pollChild(childDevice) {
|
||||
def member = state.member
|
||||
generatePollingEvents (member, childDevice)
|
||||
def member = state.member
|
||||
generatePollingEvents (member, childDevice)
|
||||
}
|
||||
|
||||
def generatePollingEvents (member, childDevice) {
|
||||
// lets figure out if the member is currently "home" (At the place)
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def urlsleeps = "https://jawbone.com/nudge/api/users/@me/sleeps"
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def urlsleeps = "https://jawbone.com/nudge/api/users/@me/sleeps"
|
||||
def goals = null
|
||||
def moves = null
|
||||
def sleeps = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
def sleeps = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
goals = response.data.data
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
moves = response.data.data.items[0]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try { // we are going to just ignore any errors
|
||||
log.debug "Member = ${member.first}"
|
||||
log.debug "Moves Goal = ${goals.move_steps} Steps"
|
||||
log.debug "Moves = ${moves.details.steps} Steps"
|
||||
log.debug "Moves = ${moves.details.steps} Steps"
|
||||
|
||||
childDevice?.sendEvent(name:"steps", value: moves.details.steps)
|
||||
childDevice?.sendEvent(name:"goal", value: goals.move_steps)
|
||||
@@ -395,29 +388,29 @@ def generatePollingEvents (member, childDevice) {
|
||||
}
|
||||
catch (e) {
|
||||
// eat it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def generateInitialEvent (member, childDevice) {
|
||||
// lets figure out if the member is currently "home" (At the place)
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def urlsleeps = "https://jawbone.com/nudge/api/users/@me/sleeps"
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def urlsleeps = "https://jawbone.com/nudge/api/users/@me/sleeps"
|
||||
def goals = null
|
||||
def moves = null
|
||||
def sleeps = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
def sleeps = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
goals = response.data.data
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
moves = response.data.data.items[0]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try { // we are going to just ignore any errors
|
||||
log.debug "Member = ${member.first}"
|
||||
log.debug "Moves Goal = ${goals.move_steps} Steps"
|
||||
log.debug "Moves = ${moves.details.steps} Steps"
|
||||
log.debug "Sleeping state = false"
|
||||
log.debug "Sleeping state = false"
|
||||
childDevice?.generateSleepingEvent(false)
|
||||
childDevice?.sendEvent(name:"steps", value: moves.details.steps)
|
||||
childDevice?.sendEvent(name:"goal", value: goals.move_steps)
|
||||
@@ -425,27 +418,27 @@ def generateInitialEvent (member, childDevice) {
|
||||
}
|
||||
catch (e) {
|
||||
// eat it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def setColor (steps,goal,childDevice) {
|
||||
def result = steps * 100 / goal
|
||||
if (result < 25)
|
||||
if (result < 25)
|
||||
childDevice?.sendEvent(name:"steps", value: "steps", label: steps)
|
||||
else if ((result >= 25) && (result < 50))
|
||||
else if ((result >= 25) && (result < 50))
|
||||
childDevice?.sendEvent(name:"steps", value: "steps1", label: steps)
|
||||
else if ((result >= 50) && (result < 75))
|
||||
else if ((result >= 50) && (result < 75))
|
||||
childDevice?.sendEvent(name:"steps", value: "steps1", label: steps)
|
||||
else if (result >= 75)
|
||||
childDevice?.sendEvent(name:"steps", value: "stepsgoal", label: steps)
|
||||
else if (result >= 75)
|
||||
childDevice?.sendEvent(name:"steps", value: "stepsgoal", label: steps)
|
||||
}
|
||||
|
||||
def hookEventHandler() {
|
||||
// log.debug "In hookEventHandler method."
|
||||
log.debug "request = ${request}"
|
||||
|
||||
def json = request.JSON
|
||||
|
||||
|
||||
def json = request.JSON
|
||||
|
||||
// get some stuff we need
|
||||
def userId = json.events.user_xid[0]
|
||||
def json_type = json.events.type[0]
|
||||
@@ -454,39 +447,39 @@ def hookEventHandler() {
|
||||
//log.debug json
|
||||
log.debug "Userid = ${userId}"
|
||||
log.debug "Notification Type: " + json_type
|
||||
log.debug "Notification Action: " + json_action
|
||||
|
||||
log.debug "Notification Action: " + json_action
|
||||
|
||||
// find the appropriate child device based on my app id and the device network id
|
||||
def externalId = "${app.id}.${userId}"
|
||||
def childDevice = getChildDevice("${externalId}")
|
||||
|
||||
|
||||
if (childDevice) {
|
||||
switch (json_action) {
|
||||
case "enter_sleep_mode":
|
||||
childDevice?.generateSleepingEvent(true)
|
||||
break
|
||||
case "exit_sleep_mode":
|
||||
childDevice?.generateSleepingEvent(false)
|
||||
break
|
||||
case "creation":
|
||||
switch (json_action) {
|
||||
case "enter_sleep_mode":
|
||||
childDevice?.generateSleepingEvent(true)
|
||||
break
|
||||
case "exit_sleep_mode":
|
||||
childDevice?.generateSleepingEvent(false)
|
||||
break
|
||||
case "creation":
|
||||
childDevice?.sendEvent(name:"steps", value: 0)
|
||||
break
|
||||
case "updation":
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def urlgoals = "https://jawbone.com/nudge/api/users/@me/goals"
|
||||
def urlmoves = "https://jawbone.com/nudge/api/users/@me/moves"
|
||||
def goals = null
|
||||
def moves = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
def moves = null
|
||||
httpGet(uri: urlgoals, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
goals = response.data.data
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
}
|
||||
httpGet(uri: urlmoves, headers: ["Authorization": "Bearer ${state.JawboneAccessToken}" ]) {response ->
|
||||
moves = response.data.data.items[0]
|
||||
}
|
||||
}
|
||||
log.debug "Goal = ${goals.move_steps} Steps"
|
||||
log.debug "Steps = ${moves.details.steps} Steps"
|
||||
childDevice?.sendEvent(name:"steps", value: moves.details.steps)
|
||||
childDevice?.sendEvent(name:"goal", value: goals.move_steps)
|
||||
//setColor(moves.details.steps,goals.move_steps,childDevice)
|
||||
childDevice?.sendEvent(name:"goal", value: goals.move_steps)
|
||||
//setColor(moves.details.steps,goals.move_steps,childDevice)
|
||||
break
|
||||
case "deletion":
|
||||
app.delete()
|
||||
@@ -499,4 +492,4 @@ def hookEventHandler() {
|
||||
|
||||
def html = """{"code":200,"message":"OK"}"""
|
||||
render contentType: 'application/json', data: html
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user