Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create example.cs #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions csharp/example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Jose; // https://github.com/dvsekhvalnov/jose-jwt
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Xml.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace HMServiceAccountAPISample;
class MainClass
{
/*
* Sandbox service account private key json example
*
* { "inserted_at":"2023-07-17T10:19:10",
* "private_key":"-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgbtaDZT7wj9kWE3tn\nkimCOiO4bKmxu74r79DNwn55UHWhRANCAATvFUpSLM6OIGfGXY001+DtKYwdTgWB\nNQF51+kiLN9OF5rRtdExRdivdNEWkcy65H5aV5dm0pB2Dl3/odUcQKgC\n-----END PRIVATE KEY-----",
* "instance_uri":"https://sandbox.api.high-mobility.com",
* "token_uri":"https://sandbox.api.high-mobility.com/v1/auth_tokens",
* "id":"6c912e9b-ac53-4068-81ac-6bf971aba0c5"
* }
*/

//For production use production service account private key json values

//Remove header, footer and newlines from your private key in the snippet
static string private_key = "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgbtaDZT7wj9kWE3tnkimCOiO4bKmxu74r79DNwn55UHWhRANCAATvFUpSLM6OIGfGXY001+DtKYwdTgWBNQF51+kiLN9OF5rRtdExRdivdNEWkcy65H5aV5dm0pB2Dl3/odUcQKgC";
//iss is id in service account private key json
static string iss = "6c912e9b-ac53-4068-81ac-6bf971aba0c5";
//For production use https://api.high-mobility.com/v1
static string aud = "https://sandbox.api.high-mobility.com/v1";

static string createJwtToken()
{
var payload = new Dictionary<string, object>()
{
{ "iss", iss },
{ "aud", aud},
{ "iat", DateTimeOffset.Now.ToUnixTimeSeconds()},
{ "jti", Guid.NewGuid()},
{ "ver", 2}
};

ECDsa key = ECDsa.Create();
key.ImportPkcs8PrivateKey(Convert.FromBase64String(private_key), out _);

string token = Jose.JWT.Encode(payload, key, JwsAlgorithm.ES256);
return token;
}

static string getServiceAccountTokenUsingJwt()
{
HttpClient client = new HttpClient();

//Add request body
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("assertion", createJwtToken()),
});

Console.WriteLine(client.DefaultRequestHeaders.ToString());
HttpResponseMessage response = client.PostAsync(aud + "/auth_tokens", formContent).Result;
string body = response.Content.ReadAsStringAsync().Result;

Console.WriteLine(response.ToString());
Console.WriteLine(body);

//Parse token from response
JObject jsonObject = JObject.Parse(body);
return jsonObject.Value<string>("auth_token");
}

static void getFleetVehicles(string token)
{
HttpClient client = new HttpClient();

//Add token as header authenticate Bearer value
client.BaseAddress = new Uri(aud);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

Console.WriteLine(client.DefaultRequestHeaders.ToString());

HttpResponseMessage response = client.GetAsync(aud + "/fleets/vehicles").Result;
string body = response.Content.ReadAsStringAsync().Result;

Console.WriteLine(response.ToString());
Console.WriteLine(body);
}

static void Main(string[] args)
{
string token = getServiceAccountTokenUsingJwt();
getFleetVehicles(token);
}
}