jubi
jubi

Reputation: 625

Http endpoint for IOT hub resource

How to get the http endpoint of an iot hub in azure. I need to build a solution to ping the http url to test whether iot hub is active or down. Similar to availability check.

Upvotes: 0

Views: 127

Answers (1)

HariHaran
HariHaran

Reputation: 4099

So you can create an Azure Function or whatever you favor, to invoke this REST API to obtain the status. Example response will be like this

{
  "totalDeviceCount": 0,
  "enabledDeviceCount": 0,
  "disabledDeviceCount": 0
}

To Authenticate against AZURE API you need to obtain the BEARER token first. Example :

private const string Resource = "https://management.azure.com/";
string authority = $"https://login.windows.net/"TenantId";
var authContext = new AuthenticationContext(authority);
var credential = new ClientCredential("ClientId", "ClientSecret");
var authResult = authContext.AcquireTokenAsync(Resource, credential).Result;
var httpClient = new HttpClient();

Now when you call any azure resource api, pass the bearer token along

httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + authResult.AccessToken);

Upvotes: 1

Related Questions