API Consuming
You can use API to configure devices, collect information and even stream part of your Boards.
- Your API URL, to access your TenantID data.
- A user account, username and password.
In order to consume API methods, from services hosted in Cloud, you need to be identified and authenticated.
This example uses
jdoe
with passwordnobody
for tenantdemo
POST a form including your credentials to the authentication URL.
authParams = { "client_id": "pub", # public authentication
"username": "jdoe",
"password": "nobody",
"grant_type": "password" }
header = {"Content-Type":"application/x-www-form-urlencoded"}
response= requests.post("https://keycloak.centiloc.com/auth/realms/demo/protocol/openid-connect/token",
data=authParams,
headers=header)
if response.status_code != 200:
print("ERR: Authentication failed")
print(response)
sys.exit(-1)
token = response.json()["access_token"]
print("token is: " + token)
using System.Net.Http;
using Grpc.Net.Client;
using Grpc.Core;
using Newtonsoft.Json;
public class AccessToken {
public string access_token { get; set; }
}
class Program
{
static void Main(string[] args)
{
var channel = GrpcChannel.ForAddress("https://demo.api.centiloc.com");
var kcParams = new Dictionary<string, string>();
kcParams.Add("client_id", "pub");
kcParams.Add("username", "jdoe");
kcParams.Add("password", "nobody");
kcParams.Add("grant_type", "password");
HttpResponseMessage response;
using (HttpClient client = new HttpClient()) {
using (var content = new FormUrlEncodedContent(kcParams))
{
content.Headers.Clear();
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
response = client.PostAsync("https://keycloak.centiloc.com/auth/realms/demo/protocol/openid-connect/token", content).Result;
}
}
if( !response.IsSuccessStatusCode ) {
Console.WriteLine("request failed");
return;
}
Console.WriteLine("KC request OK");
var rawAccessToken = response.Content.ReadAsStringAsync().Result;
string token = JsonConvert.DeserializeObject<AccessToken>(rawAccessToken).access_token;
Console.WriteLine("token is: " + token);
}
}
Imports System.Collections.Generic
Imports System.Net.Http
Imports Grpc.Net.Client
Imports Grpc.Core
Imports Centiloc.Ops.Api.Geo.Service
Imports Newtonsoft.Json
Namespace basicexample
Public Class AccessToken
Public Property access_token As String
End Class
Module Program
Sub Main(args As String())
Console.WriteLine("Hello World!")
Dim channel = GrpcChannel.ForAddress("https://demo.api.centiloc.com")
Dim kcParams As New Dictionary(Of String, String)()
kcParams.Add("client_id", "pub")
kcParams.Add("username", "jdoe")
kcParams.Add("password", "noody")
kcParams.Add("grant_type", "password")
Dim response As HttpResponseMessage
Using client As New HttpClient()
Using content = New FormUrlEncodedContent(kcParams)
content.Headers.Clear()
content.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
response = client.PostAsync("https://keycloak.centiloc.com/auth/realms/demo/protocol/openid-connect/token", content).Result
End Using
End Using
If Not response.IsSuccessStatusCode Then
Console.WriteLine("request failed")
Return
End If
Console.WriteLine("KC request OK")
Dim rawAccessToken = response.Content.ReadAsStringAsync().Result
Dim token As String = JsonConvert.DeserializeObject(Of AccessToken)(rawAccessToken).access_token
Console.WriteLine("token is: " & token)
End Sub
End Module
End Namespace
Add the token to every request to the API, in an authorization: Bearer
header.
# Create a simple slice of metadata elements containing the bearer
auth_metadata = [('authorization', "Bearer " + token)]
# Then pass this metadata to API calls.
board_handler.Get(arg, metadata=auth_metadata)
// use allmeta to pass authenticatication bearer to the API request
var allmeta = new Metadata { {"authorization", "Bearer " + token} };
// Get items on board
var itemClient = new Item.ItemClient(channel);
var noLimitFilter = new ItemFilters{Page = new ItemsPagination{Size=1000} };
var itemsResult = itemClient.GetAll(noLimitFilter, allmeta);
' use allmeta to pass authenticatication bearer to the API request:
Dim allmeta = New Metadata From {
{"authorization", "Bearer " & token}
}
' Get items on board
Dim itemClient = New Item.ItemClient(channel)
Dim onlyInFilter As New ItemFilter With {
.Type = ItemAttr.Itemstatus,
.ItemStatus = ItemStatus.In
}
Dim noLimitFilter As New ItemFilters With {
.Page = New ItemsPagination With {.Size = 1000}
}