Hi, I am currently making service manager for oauth like ecobee. So, I just followed steps in ecobee template but when at the stage of getting and setting 3rd party accessToken to atomicState (when mapped to /received). It’s not seems like recording my accessToken. Dose anyone have the same problem?
Search my post history.
If you are trying to use atomicstate in a device type, it won’t work. Smart App? Tell us more.
Of course not in device type, I was trying to use it under smart App to save the access token of 3rd party. It writes instantly but when I try to use it at the other place, like when I am trying to send the http request to the 3rd party, if I check the atomicstate, the value of access token is null. I have no idea. I will write my code flow to help you under stand.
preference {
page(start)
page(authPage)
page(done)
}
mappings {
path("/auth") { action: [ GET: “auth” ] } /get code value and make params for login./
path("/receiveToken") { action: [ GET: “receiveToken”] } /swap code and access Token/
}
def start() {
state.accessToken = null
state.authToken = null
//plz ignore above two line, its just there to make accessToken and authToken null so I can get it every time.
dynamic(next to authPage)
}
def authPage () {
if (check if state.accessToken exist and atomicstat.authToken exist and if not ) {
get accesstoken by using createaccesstoken() // now state.accesstoken is changed to new token.
redirect to get the token from this accesstoken then map to auth function.
} else {
// if authorized already
dynamic(next page to done)
}
}
def auth () {
//when maaped to here with the token now I am requesting 3rd party for the code.
redirect location: "https://3rd-party.com/authorization?${toQueryString(oauthParams)}"
// on success it will map to receivedToken function
}
def receivedToken () {
//Now I am with the code, I need to swap this code to 3rd-party's accessToken
//request 3rd-party for the accessToken
// on success
atomicState.authToken = jsonMap.access_token
//ok this is where I am having trouble, after above line I print atomicState.authToken and It is there.
//However, when it goes back to authPage( ) atomicState.authToken becomes null.
}
Could be a race condition. Does it still happen if you switch all of your "state"s to "atomicState"s?
Should be doing that in any event:
http://docs.smartthings.com/en/latest/smartapp-developers-guide/state.html#atomic-state
Are you setting the atomic state value on the same dynamic page and is that page refreshing?
Try creating a separate method that gets the value from the atomic state variable and returns it to your call.
I changed everything to atomicState and now its not even recognized if it is in different method here is the log.
debug second redirected URL: https://graph.api.smartthings.com/api/token/null/smartapps/installations/8*******-0***-4***-8***-8***********/receiveToken
debug buildRedirectUrl : {“accessToken”:null,“authToken”:null}
debug buildRedirectUrl : path = receiveToken
debug auth()
debug first redirected URL = https://graph.api.smartthings.com/api/token/9*******-0***-4***--d***********/smartapps/installations/8***-0***-4***-8***-8***********/auth
debug buildRedirectUrl : {“accessToken”:“9*******-0***-4***-****-d***********”,“authToken”:null}
debug buildRedirectUrl : path = auth
debug Start: {“accessToken”:null,“authToken”:null}
as you can see in the log, once I get redirected from the first redirected URL, accessToken becomes null again.
After redirected it will go to auth() method and rebuild another redirected URL to redirect to receivedToken method.
The funny thing is that everything worked fine 2~3 month ago…, there were no problem with it, I just came back to recheck, and its not working… so strange.
Well, not sure what do you mean?
According to the documenttion atomicState “writes to the data store when a value is set, and reads from the data store when a value is read” . So, its not holding it in the memory, its been cached to local. Why is it matter with page refreshing.
And it worked fine for 2~3months ago…
I was not trying to imply you’re doing anything wrong. It should work as before. Dynamic pages are finicky though, so my questions were along the lines of trouble shooting.
If you want, you can post or PM me your code and I can see if I can help. Also, please submit a ticket to support at support@smartthings.com. We’ll get this issue resolved.
Todd
well, it works now… I don’t know why it didn’t work in my other file. But it works in my original oauth file. welll. no idea…