Skip to content

Commit b1b6a0f

Browse files
committed
Initial commit for REST API
1 parent 36a8fd7 commit b1b6a0f

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed
Binary file not shown.
Binary file not shown.

articles/iot-central/core/tutorial-use-rest-api.md

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,27 @@ In this tutorial, you learn how to:
2929

3030
To complete the steps in this tutorial, you need:
3131

32-
* An active Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
32+
- An active Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
3333

34-
* An Android or iOS smartphone on which you're able to install a free app from one of the official app stores.
34+
- An Android or iOS smartphone on which you're able to install a free app from one of the official app stores.
3535

3636
### Azure CLI
3737

38-
You use the Azure CLI to generate the bearer tokens that some of the REST APIs use for authorization.
38+
You use the Azure CLI to make the REST API calls and to generate the bearer tokens that some of the REST APIs use for authorization.
3939

4040
[!INCLUDE [azure-cli-prepare-your-environment-no-header](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
4141

42-
### Postman
43-
44-
In this tutorial, you use [Postman](https://www.postman.com/downloads/) to make the REST API calls. If you prefer not to download and install Postman, you can use the online version. You can complete all the steps in the tutorial by using the free version of Postman.
45-
46-
The tutorial uses a predefined Postman collection that includes some scripts to help you complete the steps.
47-
48-
## Import the Postman collection
49-
50-
To import the collection, open Postman and select **Import**. In the **Import** dialog, select **Link** and paste in the following [URL](https://raw.githubusercontent.com/Azure-Samples/iot-central-docs-samples/main/postman-collection/IoT%20Central%20REST%20tutorial.postman_collection.json), select **Continue**.
51-
52-
Your workspace now contains the **IoT Central REST tutorial** collection. This collection includes all the APIs you use in the tutorial.
53-
54-
The collection uses variables to parameterize the REST API calls. To see the variables, select the `...` next to **IoT Central REST tutorial** and select **Edit**. Then select **Variables**. Many of the variables are either set automatically as you make the API calls or have preset values.
55-
5642
## Authorize the REST API
5743

5844
Before you can use the REST API, you must configure the authorization. The REST API calls in this tutorial use one of three authorization types:
5945

60-
* A bearer token that authorizes access to `https://management.azure.com`. You use this bearer token when you create and delete and IoT Central application. An IoT Central application is an Azure resource.
61-
* A bearer token that authorizes access to `https://apps.azureiotcentral.com`. You use this bearer token to create the API tokens in the IoT Central application.
62-
* Administrator and operator API tokens that authorize access to capabilities in your IoT Central application. You use these tokens for most the API calls in this tutorial. These tokens only authorize access to one specific IoT Central application.
46+
- A bearer token that authorizes access to `https://management.azure.com`. You use this bearer token when you create and delete and IoT Central application. An IoT Central application is an Azure resource.
47+
- A bearer token that authorizes access to `https://apps.azureiotcentral.com`. You use this bearer token to create the API tokens in the IoT Central application.
48+
- Administrator and operator API tokens that authorize access to capabilities in your IoT Central application. You use these tokens for most the API calls in this tutorial. These tokens only authorize access to one specific IoT Central application.
6349

64-
Assign values to the following variables in the Postman collection:
50+
Generate the bearer tokens and get the subscription ID you need to use the REST API:
6551

66-
* **bearerToken**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://management.azure.com`:
52+
- **bearerToken**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://management.azure.com`:
6753

6854
```azurecli
6955
az login
@@ -73,9 +59,9 @@ Assign values to the following variables in the Postman collection:
7359
> [!TIP]
7460
> You may need to run `az login` even if you're using the Cloud Shell.
7561
76-
Copy the `accessToken` value into the **Current value** column for **bearerToken** in the collection variables.
62+
Make a note of this management bearer token, you use it later in the tutorial.
7763
78-
* **bearerTokenApp**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://apps.azureiotcentral.com`:
64+
- **bearerTokenApp**: Run the following Azure CLI commands to generate a bearer token that authorizes access to `https://apps.azureiotcentral.com`:
7965
8066
```azurecli
8167
az account get-access-token --resource https://apps.azureiotcentral.com
@@ -84,27 +70,31 @@ Assign values to the following variables in the Postman collection:
8470
> [!TIP]
8571
> If you started a new instance of your shell, run `az login` again.
8672
87-
Copy the `accessToken` value into the **Current value** column for **bearerTokenApp** in the collection variables.
73+
Make a note of this application bearer token, you use it later in the tutorial.
8874
89-
* **subscriptionId**: Your subscription ID was included in the output from the two previous commands. Copy the `subscription` value into the **Current value** column for **subscriptionId** in the collection variables.
75+
- **subscriptionId**: Your subscription ID was included in the output from the two previous commands. Make a note of the subscription ID, you use it later in the tutorial.
9076
91-
:::image type="content" source="media/tutorial-use-rest-api/postman-variables.png" alt-text="Screenshot that shows the variables set manually in the Postman collection.":::
77+
> [!NOTE]
78+
> Bearer tokens expire after an hour. If they expire, run the same commands to generate new bearer tokens.
9279
93-
Be sure to save the changes to the Postman collection.
80+
## Create a resource group
9481
95-
> [!NOTE]
96-
> Bearer tokens expire after an hour.
82+
Use the Azure cli to create a resource group that contains the IoT Central application you create in this tutorial:
9783
98-
## Create an application
84+
```azurecli
85+
az group create --name iot-central-rest-tutorial --location eastus
86+
```
9987

100-
Use the control plane requests to create and manage IoT central applications. Use the following **PUT** request to create the application that you use in this tutorial. The request uses a bearer token to authorize and generates a random application name.
88+
## Create an IoT Central application
10189

102-
1. In Postman, open the **IoT Central REST tutorial** collection, and select the **Create an IoT central application** request.
103-
1. Select **Send**.
104-
1. Check the request succeeds. If it fails, verify that you entered the **bearerToken** and **subscriptionId** variable values in the Postman collection.
105-
1. Select **Visualize** to see the URL of your new IoT Central application. Make a note of this URL, you need it later in this tutorial.
90+
Use the following command to generate an IoT Central application with a random name to use in this tutorial:
91+
92+
```azurecli
93+
appName=app-rest-$(date +%s)
94+
az iot central app create --name $appName --resource-group iot-central-rest-tutorial --subdomain $appName
95+
```
10696

107-
:::image type="content" source="media/tutorial-use-rest-api/visualize-tab.png" alt-text="Screenshot that shows the Visualize tab with the application URL in Postman.":::
97+
Make a note of the application name, you use it later in this tutorial.
10898

10999
## Create the API tokens
110100

@@ -117,8 +107,23 @@ Use the following data plane requests to create the application API tokens in yo
117107

118108
If you want to see these tokens in your IoT central application, open the application and navigate to **Security > Permissions > API tokens**.
119109

120-
> [!NOTE]
121-
> A script in Postman automatically adds these API tokens to the list of collection variables for you.
110+
To create an operator token called `operator-token` by using the Azure CLI, run the following command. The role GUID is the ID of the operator role in all IoT Central applications:
111+
112+
```azurecli
113+
$appName=<the app name generated previously>
114+
az rest --method put --uri https://$appName.azureiotcentral.com/api/apiTokens/operator-token?api-version=2022-07-31 --headers Authorization="Bearer $bearerTokenApp" "Content-Type=application/json" --body '{"roles": [{"role": "ae2c9854-393b-4f97-8c42-479d70ce626e"}]}'
115+
```
116+
117+
Make a note of the operator token the command returns, you use it later in the tutorial. The token looks like `SharedAccessSignature sr=2...`.
118+
119+
To create an admin token called `admin-token` by using the Azure CLI, run the following command. The role GUID is the ID of the admin role in all IoT Central applications:
120+
121+
```azurecli
122+
$appName=<the app name generated previously>
123+
az rest --method put --uri https://$appName.azureiotcentral.com/api/apiTokens/admin-token?api-version=2022-07-31 --headers Authorization="Bearer $bearerTokenApp" "Content-Type=application/json" --body '{"roles": [{"role": "ca310b8d-2f4a-44e0-a36e-957c202cd8d4"}]}'
124+
```
125+
126+
Make a note of the admin token the command returns, you use it later in the tutorial. The token looks like `SharedAccessSignature sr=2...`.
122127

123128
## Register a device
124129

0 commit comments

Comments
 (0)