HMRC Hello Application


namespace consoleapp1 
{
  public class response1
  {
    public string? access_token { get; set; }
    public string? scope { get; set; }
    public int expires_in { get; set; }
    public string? token_type { get; set; }
  }

  class Program
  {
    private static readonly
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();

    //--------------------------------------------------------------------------------------
    static async
    System.Threading.Tasks.Task
    Main(string[] args)
    {
      var response = await Method_HelloApplication();
      Console.WriteLine(response);
    }
    //--------------------------------------------------------------------------------------
    private static async
    System.Threading.Tasks.Task<string>
    Method_GetAccessToken()
    {
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(
          new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

      var parametersObject = new
      {
        client_id = "********",
        client_secret = "********",
        scope = "hello",
        grant_type = "client_credentials",
        redirect_uri = "https://localhost:8081"
      };
      var parametersJSON = System.Text.Json.JsonSerializer.Serialize(parametersObject);
      System.Net.Http.StringContent _httpCONTENT = new System.Net.Http.StringContent(parametersJSON.ToString(), System.Text.Encoding.UTF8, "application/json");
      System.Net.Http.HttpResponseMessage response =
      await client.PostAsync("https://test-api.service.hmrc.gov.uk/oauth/token", _httpCONTENT);

      string response_string =
      await response.Content.ReadAsStringAsync();
      return response_string;
    }
    //--------------------------------------------------------------------------------------
    private static async
    System.Threading.Tasks.Task<string>
    Method_GetApplicationResponse(string AccessToken)
    {
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(
          new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.hmrc.1.0+json"));

      client.DefaultRequestHeaders.Authorization =
          new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AccessToken);

      System.Net.Http.HttpResponseMessage response =
      await client.GetAsync("https://test-api.service.hmrc.gov.uk/hello/application");

      string response_string =
      await response.Content.ReadAsStringAsync();
      return response_string;
    }
    //--------------------------------------------------------------------------------------
    private static async
    System.Threading.Tasks.Task<string>
    Method_HelloApplication()
    {
      string _tokenResults = await Method_GetAccessToken();

      if (_tokenResults != null)
      {
        var responseObject = System.Text.Json.JsonSerializer.Deserialize<response1>(_tokenResults);
        if (responseObject != null)
        {
          string _accessToken = responseObject.access_token ?? "";

          if (_accessToken != null)
          {
            string _getapplication = await Method_GetApplicationResponse(_accessToken);

            if (_getapplication != null)
            {
              return _getapplication;
            }
          }
        }
      }
      return "";
    }
    //--------------------------------------------------------------------------------------
  }
}



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext