Example C# script

This example application shows how to use the Itron Identity Server to get a token and then use that token to make a request to the API.

Copy
using System;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http.Headers;
using System.Text.Json;

//This example application shows how you can use the Identity Server to get a token and then use that token to make a request to the API

namespace MyApp 
{
    public class Token
    {
        public string access_token { get; set; }
        public JwtSecurityToken jwtSecurityToken { get; set; }
    }
    public class BatchToken
    {
        public string name { get; set; }
        public string type { get; set; }
        public string @default { get; set; }
    }
    public class BatchQueryResponse
    {
        public BatchToken[] tokens { get; set; }
    }
    internal class Program
    {
        private static readonly HttpClient client = new HttpClient();
        private static Token token;

        private static readonly Guid data_product_id = new Guid("2211D5AC-87D6-45DC-94CE-B82ADA1037DF"); //Provided by Itron
        private static readonly Guid subscription_id = new Guid("2E5C5952-29D6-4D85-B25A-4A53226BB6FA"); //Provided by Itron

        private static readonly string clientId = "[Provided by itron]";
        private static readonly string clientSecret = "[Provided by itron]";
        private static readonly string scope = "DPSSApi";
        private static readonly string tokenUrl = "https://idenserver.itrontotal.com/connect/token";
        private static readonly string base_api_url = "https://k8s.itrontotal.com/dpss/api/v1";

        //Caches a token and returns it if it is still valid, otherwise gets a new one
        public static async Task<Token> GetTokenIfNecessary()
        {
            //If we don't have a token or it is expiring, get a new one
            if (token == null)
            {
                  token = await GetToken();
            }
            else if (token.jwtSecurityToken.ValidTo < DateTime.UtcNow.AddMinutes(-1))
            {
                token = await GetToken();
            }
            return token;
        }

        //Get a token from the Identity Server
        private static async Task<Token> GetToken()
        {
            //These values should come from an outside configuration source and not be hard coded as this example is
            var values = new Dictionary<string, string>
            {
                { "client_id", clientId },
                { "client_secret", clientSecret },
                { "scope", scope },
                { "grant_type", "client_credentials" }
            };
            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync(tokenUrl, content);
            var responseString = await response.Content.ReadAsStringAsync();
            var token =  JsonSerializer.Deserialize<Token>(responseString);            
            token.jwtSecurityToken = new JwtSecurityToken(token.access_token);
            return token;
        }

        static void Main()
        {
            //Get a token
            var token = GetTokenIfNecessary().Result;
            
            //Make a request to the API
            var request = new HttpRequestMessage(HttpMethod.Get, $"{base_api_url}/data-products/{data_product_id}/subscriptions/" +
                $"{subscription_id}/batch-request-parameters");
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            var response = client.SendAsync(request).Result;
            var responseString = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine(responseString);

            BatchQueryResponse batchQueryResponse = JsonSerializer.Deserialize<BatchQueryResponse>(responseString);
            Console.WriteLine(batchQueryResponse.tokens[0].name);

        }
    }
}