element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Industrial Automation
  • Technologies
  • More
Industrial Automation
Blog Connect to AVNET iotconnect.io - Automatic Provisioning with REST API
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Industrial Automation to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 29 May 2021 11:37 AM Date Created
  • Views 1483 views
  • Likes 3 likes
  • Comments 0 comments
  • iiot
  • rest
  • iot
  • iotconnect.io
Related
Recommended

Connect to AVNET iotconnect.io - Automatic Provisioning with REST API

Jan Cumps
Jan Cumps
29 May 2021

Cloud providers show you how to provision devices and set up your IoT environment from a portal.

It's the best way to get started. But it doesn't scale. If your organisation sells many devices, the process should be automated. Safely.

To enable that, many (at least AWS and Azure) have a scriptable interface too. Usually a REST API.

I'm investigating the Avnet IoTConnect REST API - a solution that is hosted on Azure.

 

image

 

I use their example application. This example can provision a new device automatically, without user involvement.

I'm going to do 3 of these steps, to learn and prove that I can use the API:

  • open a session
  • authenticate
  • execute one call that requires being authenticated

 

Open a Session and Authenticate

 

This is the Python code snippet from that Avnet example to get a valid session:

 

def get_auth(username, password, solution_key):
    try:
        access_token = None
        data = None
        authToken = service_call("GET", AUTH_BASEURL + "/auth/basic-token")
        if authToken != None:
            data = str(authToken["data"])
        if data != None:
            body = {}
            body["username"] = username
            body["password"] = password
            header = {
                "Content-type": "application/json",
                "Accept": "*/*",
                "Authorization": 'Basic %s' % data,
                "Solution-key": solution_key
            }
            data = service_call("POST", AUTH_BASEURL + "/auth/login", header, body)
            if data != None:
                access_token = str('Bearer %s' % data["access_token"])
        return access_token
    except:
        return None

 

The first step is to get a basic token. Then use that, with your account and environment details, to get access (and an access token).

 

These are constants used in the exercise

 

http_auth_token = https://auth.iotconnect.io/api/v1.1
http_auth_login = https://auth.iotconnect.io/api/v1.1
http_device_create = https://device.iotconnect.io/api/v1.1
http_device_template = https://device.iotconnect.io/api/v1.1
http_rule_template = https://device.iotconnect.io/api/v1.1
http_event_template = https://event.iotconnect.io/api/v1.1
http_user_template = https://user.iotconnect.io/api/v1.1
http_device_firmware = https://firmware.iotconnect.io/api/v2

 

I'm using the online REST client REQBIN to interactively run the activities:

 

Start Session

 

First I'm retrieving a basic token.

I used the AUTH_BASEURL with /auth/basic-token appended, GET.

 

image

 

image

 

After pressing Send, I got this reply:

 

image

 

{
    "data": "cHJvZDp2aXVnZG1mdHplcWRuaWdlY3lqbHlhbnFiaWV3amJ4cWZsYnhtcWtmdmRneGNmYWpxY2d1bHVrdnZkZnVpamp4Z2F4Y2hpcm1uZHF1dXBudml1Z2d2ZGVsYWdjamtweGx0anFj",
    "status": 200,
    "message": "Basic token has been generated successfully"
}

 

The data is the basis token that we'll use to get authenticated. Status 200 means: OK.

 

Authenticate

 

With that info, we go to step 2: authenticate.

The data received in the previous step is our authentication token.

image

The JSON payload is username and password.

 

image

The solution key, a value that is shared by Avnet (I used the one from a sample application) has to be set as a raw header (no quotes).

image

 

The image below shows the summary of all data to be posted, and the reply.

 

image

 

The reply, in JSON format:

 

{
    "token_type": "bearer",
    "access_token": "e********o",
    "expires_in": 86400,
    "refresh_token": "m**********************t",
    "status": 200
}

 

The status 200 means we have a successful, authenticated session. We can now use it to retrieve info, and use the REST API to provision devices.

Because all of this runs with structured calls and structured replies, this can be automated with scripts or a program.

 

Perform an API call.

 

A good proof of concept is to execute an REST API call that needs an active and authenticated session.

I'm going to query the IoTConnect template for my device.

 

Python code of the Avnet sample:

 

def get_template(searchText):
    global ACCESS_TOKEN
    try:
        header = {
            "Content-type": "application/json",
            "Accept": "*/*",
            "Authorization": ACCESS_TOKEN
        }


        templates = []
        response = service_call("GET", TEMPLATE_BASEURL + "/device-template?searchText=%s" % searchText, header)
        if response != None and response["data"] != None and len(response["data"]) > 0:
            templates = response["data"]
        
        if len(templates) > 0:
            return templates[0]
        else:
            return None
    except:
        return None

 

I'm using a different url, because this is part of the device API, not the authentication API.

I'm querying to see if the template that my device should be assigned to, exists.

For the curious reader, I'm reviewing IoTCloud's RSL10 demo. Here I try to retrieve the template for that example.

image

 

When I authenticated, I got a bearer token. That's now used to proceed:

image

 

 

This is the result of executing that query:

 

image

 

 

{
    "data": [{
        "guid": "2***-****4",
        "code": "RSL10Final",
        "name": "RSL10Final",
        "description": "For ON Semi RSL10 Beacon Demo",
        "firmwareGuid": "9*-*-*-*-6",
        "createdDate": "2021-02-11T15:47:19.720Z",
        "updatedDate": "2021-04-01T14:51:55.913Z",
        "attributeCount": 2,
        "settingCount": 0,
        "commandCount": 0,
        "deviceCount": 73,
        "ruleCount": 7,
        "isValidateTemplate": true,
        "isValidEdgeSupport": true,
        "isValidType2Support": true,
        "tag": null,
        "authType": 1,
        "isEdgeSupport": false,
        "gatewaySupport": false,
        "isLowBandwidth": false,
        "isIotEdgeEnable": false,
        "isSolutionTemplate": false,
        "solutionName": null
    }, {
        "guid": "***-****-****",
        "code": "RSL10Spher",
        "name": "RSL10FinalSphere",
        "description": "For ON Semi RSL10 Beacon Demo",
        "firmwareGuid": null,
        "createdDate": "2021-04-06T17:06:57.890Z",
        "updatedDate": "2021-04-06T17:06:57.890Z",
        "attributeCount": 2,
        "settingCount": 0,
        "commandCount": 0,
        "deviceCount": 43,
        "ruleCount": 0,
        "isValidateTemplate": true,
        "isValidEdgeSupport": true,
        "isValidType2Support": true,
        "tag": null,
        "authType": 2,
        "isEdgeSupport": false,
        "gatewaySupport": false,
        "isLowBandwidth": false,
        "isIotEdgeEnable": false,
        "isSolutionTemplate": false,
        "solutionName": null
    }],
    "status": 200,
    "message": "Device template list loaded successfully",
    "count": 2
}

 

This cycle of 3 calls confirms the proof of concept, from session initiation, over authentication, up to talking to the IoTConnect Cloud solution.

The approach is similar for other providers. It's a skill you can reuse.

 

 

The Python SDK with On Semiconductor RSL10 BLE article seriesIndustry
part 1: overview and goal
part 2: WiFi Provisioning
part 3: Adding a Module (RSL10)
part 4: Talk BLE to the On Semi RSL10 Sensor Kit
part 5: A Cloud User Experience Example
part 6: Register as a Gateway Device
part 7: Register a Gateway and Client Devices
The NODE-Red SDK article seriesIndustry
part 1: overview and goal
register a Thing and connect to IoTConnect.io cloud
part 2: create an account and log on to the portal
part 3: set up the thing and its interface in the cloud
part 4: set up Node-RED and first exchange
interact with IoTConnect.io cloud
part 5: online dashboard
part 6: rules and alerts
part 7: messages and commands from the cloud
safer connections with certificates
part 8a: safer connect with Self Signed Certificates
part 8b: safer connect with CA certificatesY
commercial and industrial scale: outsource certificate generation and programming to subcontractors and suppliers
part 9a: Outsource Certificate Signing in IIoT Supply ChainY
part 9b: IIoT supply chain and Certificates - Create Ca Root certificate, Load to IoTConnect Cloud and ValidateY
part 9c: IIoT supply chain and Certificates - Create an Intermediate CA Certificate for your SubcontractorY
part 9d: IIoT supply chain and Certificates - Subcontractor Generates a Thing Certificate for Your DeviceY
part 9e: IIoT supply chain and Certificates - Test!Y
commercial and industrial scale: Trusted Platform Module (TPM) Authentication
part 10: Trusted Platform Module (TPM) SecurityY
Infineon SLx9670 Trusted Platform Module (TPM) for IoT SecurityY
The Automate Device Provisioning and Cloud Configuration article seriesIndustry
Automatic Provisioning with REST APIY
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube