// Create new project with command: dotnet new console -n GetToken
// cd into the project directory: cd GetToken
// Copy this code and replace the created Program.cs with it
// Replace the placeholders with your credentials
// Install package: dotnet add package Newtonsoft.Json
// Run with command: dotnet run

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

class Program
{
    static async Task Main(string[] args)
    {
        // Fill in your credentials below
        var clientId = "REPLACE-ME-WITH-CLIENT-ID";
        var clientSecret = "REPLACE-ME-WITH-CLIENT-SECRET";
        var scope = "REPLACE-ME-WITH-SCOPE";

        // Get access token
        var tokenUrl = "https://id.talenom.com/api/b2b/oauth2/v1.0/token";
        
        using (var client = new HttpClient())
        {
            var tokenData = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("grant_type", "client_credentials"),
                new KeyValuePair<string, string>("client_id", clientId),
                new KeyValuePair<string, string>("client_secret", clientSecret),
                new KeyValuePair<string, string>("scope", scope)
            });
            
            tokenData.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");

            var tokenResponse = await client.PostAsync(tokenUrl, tokenData);
            var tokenContent = await tokenResponse.Content.ReadAsStringAsync();
            var tokenJson = JObject.Parse(tokenContent);
            var accessToken = tokenJson["access_token"].ToString();
            
            Console.WriteLine("Access token: " + "Bearer " + accessToken);
            Console.WriteLine("Copy the token above to your clipboard for use in the portal!");
        }
    }
}