REST API - Intermittent Invalid Token

Hello Cuba Community,

We setup the REST API add on for a project. Everything seems to be working well. We noticed one issue that I am curious if someone else has run into before. When calling the API from curl we get a valid token and we can use that token to request/return data. It seems to be intermittent though.

Here is an output from the terminal. Lines with “[]” were valid API responses with data returned (removed returned data).
As you see from the curl output sometimes we return data sometimes we get an invalid token using the same request in succession.

Would the fact that the app is deployed to multiple instances matter?

If so would it be better to persist the token to the database?

Any other possibilities?

Curl Output

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}
C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_APIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_CoralAPIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_CoralAPIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
{"error":"invalid_token","error_description":"Invalid access token: 15fbbd58-812c-47e9-8115-bba6b917555f"}

C:\Users\smith>Curl -X GET -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Bearer 15fbbd58-812c-47e9-8115-bba6b917555f" "https://somesite.com/rest/v2/services/registry_CoralAPIService/getRegistryRecordByAccessionID?accessionID=02d0156c-167f-b0e0-b004-edb77d057d16"
[]

Appreciate the assistance.

Hi,

Of course it matters.

If you deploy multiple instances of the CUBA application, you need to configure JGroups for cluster communication. This will solve the mentioned problem with REST.
https://doc.cuba-platform.com/manual-7.2/cluster_mw_server.html

The alternative solution would be to turn on the cuba.rest.storeTokensInDb property to enable storing tokens in DB. But note that JGroups configuration is necessary anyway for any multi-node CUBA application. I’ve answered why in this topic: Cluster with middleware and client in the same application server - #2 от пользователя albudarov

1 Like

Thank you Alexander! - This was exactly it. Once I setup jgroups I received consistent responses. Appreciate your help.