Fetching data from MobiControl with a Snap App
Summary
Introduction
Sometimes users want to tightly integrate SOTI Snap and MobiControl. For example, when you need to verify in an app if a particular device is online or when was the last check-in time, etc. There can be various scenarios on why you would want to bring in data from MobiControl into a Snap app depend
Related SOTI ONE Platform Products
Process Description
Introduction
Sometimes users want to tightly integrate SOTI Snap and MobiControl. For example, when you need to verify in an app if a particular device is online or when was the last check-in time, etc. There can be various scenarios on why you would want to bring in data from MobiControl into a Snap app depending on your scenario.
In this tutorial we will be showing how to fetch some basic information from a MobiControl instance into a Snap app. We will create a simple application in Snap with two textboxes and a button that, when clicked, will trigger our blockly event to get some information from a MobiControl instance.
Prerequisites
- Access to a Snap instance
- Access to a MobiControl instance
- Having MobiControl API credentials
- A Mobile device enrolled to Snap
Creating Application in Snap that can Fetch Data from MobiControl
Step 1
To get started, go ahead and create a brand-new app. Select the form and, on the properties tab on the right, toggle off Automatic Navigation.
Next, select the submit button that was automatically created when you created the application, navigate to events tap and remove all existing actions.
Rename the labels and the button text of your app accordingly. Here is the example we will be using with two labels, two textboxes and a button:
Now we will be creating a variable to store the API authentication token. To do so, navigate to Data Connections Rule > Global Variables, select New Variable, enter “token” for the variable name, and leave retain value toggled off.
Go back to the project, select the first page of the form, and navigate to the Events tab on the top right corner. We will add an event to execute on page load. In other words, the event will run every time a user opens this page on the device.
Notice that under Events, “On Load” event is selected. Click on Add button, choose Run Blockly from the list and click Blockly Editor
We will build the API request to get the authentication token from MobiControl. This token will be used in all our subsequent requests to authenticate with MobiControl server.
First, name your blockly script on the top left corner and add the Rest Service block to the canvas.
Change the request method to POST and enter the token URL endpoint like https://server.domain.tld/MobiControl/api/token. Check MobiControl API documentation for more information.
Next enter the Client ID as the username and Client secret as the password in the Authentication tab. (The ClientID and Client secret are provided when you create the MobiControl API credentials. Contact SOTI Support for more details if needed).
Once you enter the username and password, move to the Data tab of the REST block and add the following properties:
Also, change the data encoding type to x-www-form-urlencoded. This is what it looks like on our sample app. Example:
Now click the test button to test the connection and once you get the response click done.
Add a set global variable block into the on-success section of the REST block, then expand the response object, search for the token, and assign it to the global variable we created previously.
Lastly, save the script and exit blockly IDE.
Step 2
We will now add an event to the button to request the device information. To do this, select the button and navigate to Events > Run Blockly > Blockly Editor. In the editor, create a local variable named “localToken” and assign the content of the token global variable to it, like so:
Next, we will be making an API call to get the device information using the ‘localToken’ variable we previously created. So, add a REST block to the canvas, select GET method, and enter the device information endpoint. The URL follows the following format: https://your_domain_here/mobicontrol/api/devices/device_id
Here is my entire blockly script for the button:
Notice two things:
In the request Headers, I added an authorization property with the ‘localToken’ variable preceded by the word “bearer “as a value.
I mapped the DeviceName and LastCheckInTime parameters from the response object to the two textboxes I have available in my application in the on success section of the block.
Once you publish and run the application on the device, you can click on the button to get the Device Name and CheckIn Time to the textboxes in the app.
Was this helpful?
Thanks for your feedback