Callback token not working

Hello,
I’m having issue since January in my device integration in ST.

The flow to add the device I follow is this one:

  1. The user adds my device on the ST app
  2. An AWS Cognito login page appears to enstablish the OAUTH 2.0 login
  3. After correct login the following requests are made to my wenhook:
  • grantCallBackAcess during which I save the call back code on my database
  • discoveryRequest
  • stateRefresh , during this phase I use the call back code to make an accessTokenRequest to https://c2c-eu.smartthings.com/oauth/token using the following data:
    {
    “headers”: {
    “schema”: “st-schema”,
    “version”: “1.0”,
    “interactionType”: “accessTokenRequest”,
    “requestId”: “abc-123”
    },
    “callbackAuthentication”: {
    “grantType”: “authorization_code”,
    “code”: “the-just-generated-call-back-code”,
    “clientId”: “my-client-id”,
    “clientSecret”: “my-client-secret”
    }
    }

My problem is the response I get here: 401 Unauthorized
{
“headers”: {
“schema”: “st-schema”,
“version”: “1.0”,
“interactionType”: “accessTokenRequest”,
“requestId”: “cc63ff54-ec1f-466e-a7ca-2fd65e8494d7”
},
“globalError”: {
“errorEnum”: “INVALID-CODE”,
“detail”: “the provided code is invalid”
}
}

This request used to work fine, i could then get the acces and refresh token for my integration, save them on my db and than use them to push data to smart things from my backend with subsequent stateRefresh requests. When the acces token expired I used to make accessTokenRefresh request to refresh the access and refresh token on my db and keep the comunication working.

Do you have any clue on why theese requests are not working anymore? Since i can not make the first auth token request all the process i broken, my users can’t see their data updated and so they can not use automations and routines

Tagging @nayelyz

1 Like

Hi @emanuele.d1994
Are you using the schema SDK or are you handling all the interactions yourself?

Deleted by author

I’m handling the interactions myself i guess, my backend is in Java and I’m using retrofit to handle http requests

I noticed that i couldn’t save the access and refresh token generated during the first granCallBackAccess request because on my db i had the column access and refresh token with a maximum of 255 charaters, I think that the tokens got longer during last month because now I increased the columns size and the tokens are correctly saved.

So I think this problem is solved if the refreshAccessTokenRequest works fine.

I was thinking to build a procedure to update all my users token once or twice a day to keep the comunication up, do you think is a good idea or is better to keep refreshing the access token only after their expiration?

The access token is valid for 24 hours, so updating it twice a day would consume unnecessary resources. Our SDK checks for a 401 response when making an update, refreshes the token, and retries the request. This approach might be useful as a reference for your implementation. You can find more details here: st-schema-nodejs/lib/callbacks/StateUpdateRequest.js at master · SmartThingsCommunity/st-schema-nodejs · GitHub

StateUpdateRequest.js

if (res.status === 401 && refreshedCallback)

Since I’m using Java i can’t use the javascript sdk, i’m using Smart Things APIs and handling requests and response , but I think what I’m doing is quite similar, when my state refresh fails due to authentication i make a refreshAccessoToken request

1 Like