cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Invalid API Key when making REST call

kbetances
2-Explorer

Invalid API Key when making REST call

My REST API call to query my datatable keeps returning the following error in the logs: 

Error [authenticating connection], Server websocket will be closed. cause: Invalid Application Key c.t.c.s.c.j.Jsr356ServerConnection http-nio-8181-exec-86

This is my REST call:

http://<myHost>/Thingworx/Things/<myDataTable>/Services/QueryDataTableEntries?method=post&userid=<User>r&password=<Password>&appKey=<my Api Key>&x-thingworx-session=true&content type=application/json&Accept=application/json&maxItems=10&query=<encoded json param>

I double checked that I indeed copied the api key correctly and that the key is associated with the user I am passing in.

When I pass in a simple json query I get the following warning in my logs instead but all I get results back.

Log:

Server WebSocket closed unexpectedly and has already been unregistered from Server Endpoint [ws session id: 5a1e7] An established connection was aborted by the software in your host machine

Json Query:

{"filters":{"type": "And","filters":[{"type": "OR","filters":[{"type": "LIKE","fieldName": "ASSETCODE","value":"Pump"},{"type": "LIKE","fieldName": "ASSETDESC","value": "Pump"},{"type": "LIKE","fieldName": "ASSETCLASS","value": "Pump"},{"type": "LIKE","fieldName": "CLASSDESC","value": "Pump"},{"type": "LIKE","fieldName": "MANUFCODE","value": "Pump"},{"type": "LIKE","fieldName": "MFGDESC","value": "Pump"},{"type": "LIKE","fieldName": "MFGMODEL","value": "Pump"},{"type": "LIKE","fieldName": "ASSETCATE","value":"Pump"},{"type": "LIKE","fieldName": "CATEDESC","value": "Pump"},{"type": "LIKE","fieldName": "ORG","value": "Pump"}]}]}}

But when I do a compound query like this:

{"filters":{"type": "And","filters":[{"type": "OR","filters":[{"type": "LIKE","fieldName": "MFGMODEL","value":"Pump"},{"type": "LIKE","fieldName": "ASSETCODE","value": "Pump"},{"type": "LIKE","fieldName": "ASSETDESC","value": "Pump"},{"type": "LIKE","fieldName": "ORG","value": "Pump"}]},{"type": "OR","filters":[{"type": "IN","fieldName": "ORG","values":["ALB","KAL"]}]}]}}

I get the invalid api key log mentioned above and my connection times out without returning anything.

Any advice is much appreciated.

8 REPLIES 8
PaiChung
22-Sapphire I
(To:kbetances)

First of all, in this case, don't use Session = True because you don't need a session and you will be spinning of sessions needlessly.

Second, check PlatformSubsystem configuration to see if it allows the appkey in URL or not.

Thingworx now by default blocks this, because it is recommended that the appkey is passed in the header not on the url for security reasons.

Thanks! I checked all this and all seems okay. It must be something with my query. Something like this isn't working for me but I cant figure out why.

{

     "filters":

     {

          "type": "And",

          "filters":

          [

               {

                    "type": "OR",

                    "filters":

                    [

                         {

                              "type": "LIKE",

                              "fieldName": "MFGMODEL",

                              "value":"Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETCODE",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETDESC",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ORG",

                              "value": "Pump"

                         }

               ]

         },

          {

               "type": "OR",

               "filters":

               [

                    {

                         "type": "IN",

                         "fieldName": "ORG",

                         "values":["NAG","KAL"]

                    }

               ]

          }

   ]

}

}

dupatel
19-Tanzanite
(To:kbetances)

Kelvin Betances: Could you please confirm the ThingWorx version you are using. Please follow below steps and see it it help resolve the issue:

1. Enable or Tick the configuration "Allow Request Method Switch" going to Subsystem -> Platform Subsystem . Please note this setting expose details to external system so needs to be review correctly from security perspective.

2. Make sure the expiry of the app key is future date and is valid.

-Durgesh

kbetances
2-Explorer
(To:dupatel)

I have Thingworx version 8.

I checked both things and it seems to be fine. It has to be my query but I cant find the syntax error:

{

     "filters":

     {

          "type": "And",

          "filters":

          [

               {

                    "type": "OR",

                    "filters":

                    [

                         {

                              "type": "LIKE",

                              "fieldName": "MFGMODEL",

                              "value":"Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETCODE",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ASSETDESC",

                              "value": "Pump"

                         },

                         {

                              "type": "LIKE",

                              "fieldName": "ORG",

                              "value": "Pump"

                         }

               ]

         },

          {

               "type": "OR",

               "filters":

               [

                    {

                         "type": "IN",

                         "fieldName": "ORG",

                         "values":["NAG","KAL"]

                    }

               ]

          }

   ]

}

}

rnayak-2
12-Amethyst
(To:kbetances)

Kelvin Betances in your query the first filter has a type 'And' which should be "AND", rest looks good. Once you make that update run this within Composer and see if you do have a data that matches this filter! Once you know the response withing composer then try running a REST service externally and see if you still see error messages in the log along with expected result in the output

Thanks!

I have corrected this but when I try to run this updated query, I am stuck forever on the Executing screen...

How do I check the logs for service calls? When I check the application logs I see the following every few seconds.

Error [authenticating connection], Server websocket will be closed. cause: Invalid Application Key

c.t.c.s.c.j.Jsr356ServerConnection

http-nio-8181-exec-80

rnayak-2
12-Amethyst
(To:kbetances)

I think that error may be because of some other service or agent trying to access TWX with an incorrect App Key. Check if there are other services (through mashup auto refresh etc) or edge agents running which is causing these log messages.

If you are stuck in the composer executing screen then it means you have an issue with your query. Without knowing the data structure/data in your table its hard to say what is causing the issue. But upon looking closely it looks like you have an 'AND'  condition for a resultant set of {ORG=%Pump%  other conditions } and ORG = ["NAG","KAL"]. So technically you are expecting your result to have both Pump and Nag Kal in ORG field in some cases. I dont know if thats a valid scenario based on your data. Also Why do you have a second nested 'OR' before IN condition as there is only one filter in it?

Try splitting these conditions and run query 1st and slowly build the query by merging the fields one by one and see if you can find the field causing the issue within filter. Also whats the size of your Data table or stream ? If it is large then it will take time as the entire dataset has to be loaded in memory 1st and then apply this filter ! Hope this helps

vxavier
14-Alexandrite
(To:kbetances)

You are using Post, sometimes I had to, in the app key, enable to the user execute services.

Announcements


Top Tags