Desktop application example (External/mobile client type)

This example shows external/mobile authorization workflow that involves using a web browser and a callback URL. It also shows to how refresh an access token and query protected resources. It doesn't use Trakopolis API client library to show you all internal details. Download External/Mobile app example

Command line application example (PrivateM2M client type) using Trakopolis API client

This example uses official Trakopolis API client library. You will have to update the client Id and client secret in TrakopolisClientSample.exe.config to connect to the API server. Download PrivateM2M Client C# Example

Command line application Example (Private M2M client type)

This example doesn't use official Trakopolis API client library to show you all internal details of OAuth2 authorization.  You will have to update the client Id and secret to connect to the API server.

using System;
 using System.IO;
 using System.Net;
 using System.Text;
 using System.Web.Script.Serialization;
 namespace ApiManualClientSample
 {
     class Program
     {
         static void Main()
         {
             const String clientIdentifier = "Enter your client Id";
             const String clientSecret = "Enter your client secret";
             const String accessTokenEndpointUrl = "https://api.trakopolis.com/api/oauth2/token";
             const String resourceEndpoint = "https://api.trakopolis.com";
             try
             {
                 // 1. Get an access token.
                 var tokenRequest = WebRequest.Create(accessTokenEndpointUrl);
                 tokenRequest.Method = "POST";
                 tokenRequest.ContentType = "application/x-www-form-urlencoded";
                 string credentials = String.Format("{0}:{1}", clientIdentifier, clientSecret);
                 tokenRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials)));
                 var tokenJson = GetResponse(tokenRequest, "grant_type=client_credentials");
                 var serializer = new JavaScriptSerializer(); // This is JSON deserializer.
                 var accessToken = serializer.Deserialize<TokenInfo>(tokenJson).access_token;
                 // 2. Use access token to read something.
                 var assetsRequest = WebRequest.Create(resourceEndpoint + "/api/assets");
                 assetsRequest.Headers.Add("Authorization", "Bearer " + accessToken);
                 assetsRequest.Headers["request-type"] = "api";
                 assetsRequest.Headers["response-type"] = "application/json"; // Use JSON, you can choose XML as well.
                 assetsRequest.Headers["result-size"] = "5"; // restrict result size to 5. By default most entities have limit = 1000
                 assetsRequest.ContentType = "application/json";
                 var assets = GetResponse(assetsRequest);
                 Console.WriteLine("Assets retrieved (5 max). JSON: " + Environment.NewLine + assets);
             }
             catch (Exception exc)
             {
                 Console.WriteLine("An error occured: " + exc);
             }
             Console.WriteLine("Press any key to exit.");
             Console.ReadKey();
         }
         private static string GetResponse(WebRequest request, String postData = null)
         {
             if (!String.IsNullOrWhiteSpace(postData))
             {
                 // Get bytes of data and send it.
                 byte[] bytes = Encoding.UTF8.GetBytes(postData);
                 request.ContentLength = bytes.Length;
                 using (Stream newStream = request.GetRequestStream())
                     newStream.Write(bytes, 0, bytes.Length);
             }
             // Get response
             string responseContent;
             using (var response = (HttpWebResponse)request.GetResponse())
             {
                 var stream = response.GetResponseStream();
                 if (stream == null)
                     return null;
                 responseContent = new StreamReader(stream).ReadToEnd();
             }
             return responseContent;
         }
     }
     class TokenInfo
     {
         public String access_token { get; set; }
         // There are more properties in this class, but they are omited to make this code simple.
     }
 }
 					
This language is not supported or no code example is available.