4 Replies Latest reply on Aug 7, 2014 10:56 PM by adam RSS
    saurabhs Newbie

    Application Key Creation from Service

    description: undefined /* STRING /,</p><p class="MsoNormal"><span class="Apple-tab-span" style="white-space:pre">          </span>name: user / STRING */,

      • Application Key Creation from Service
        adam Creator

        Hi Saurabh,

         

        To create entities, you need to do three things:
        • Restart the entity. Once you've enabled the entity, the last step required is to restart the entity using its own RestartThing() Service.
        Whenever you're programmatically creating entities, it's important to use proper error handling as uncaught exceptions will result in "ghost" entities being created which can only be removed by restarting Tomcat. You'll want to include try-catch blocks in your Services when appropriate.
        • Create the entity. ThingWorx provides Services for creating various entity types (Things, ThingTemplates, Applications Keys, etc.) programmatically.

        Regards,
        Adam


        • Application Key Creation from Service
          saurabhs Newbie

          Thanks Adam..

           


          Do you have a off the shelf code available for the same which I can use. It would be helpful..


          Thanks

          Saurabh



          • Application Key Creation from Service
            saurabhs Newbie

            Hi Adam

             


            I tried to recreate what you said and here is my code snippet. But I am getting the exception.


            var params = {

             

            description: undefined /* STRING /,</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>name: usler / STRING /,</div><div>        user: UserName / USERNAME /,</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>whitelist:"abc"  / STRING */

            };


            Resources["EntityServices"].CreateApplicationKey(params);

            me.EnableThing();

            me.RestartThing();


            I am still getting the following exception and the url doesnt understand the App key



             

            00:35:44 ERROR - Error in invoking service Things_AppKeyThing, generateAppKeyForUser, Things, AppKeyThing, Services, generateAppKeyForUser. Error from the server: "Wrapped com.thingworx.common.exceptions.InvalidRequestException: ApplicationKey already exists Cause: ApplicationKey already exists"

            </span></div></p>

              • Application Key Creation from Service
                adam Creator

                Saurabh,

                 

                It's likely you have a "ghost" testerror

                Thing

                 and you will need to restart Tomcat to remove it. In your

                Service

                , you'll want to wrap everything that could throw an exception in a try-catch block. For example:


                try {

                  // Create Application Key

                }


                catch (err) {

                  logger.error("Oh no!");

                  // Remove Application Key in Memory (if necessary)

                }