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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Application Key Creation from Service

saurabhs
1-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 */,

4 REPLIES 4
adam11
5-Regular Member
(To:saurabhs)

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


Thanks Adam..


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


Thanks

Saurabh



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>

adam11
5-Regular Member
(To:saurabhs)

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)

}



Top Tags