To be able to use the functionality provided in the REST API it is necessary to obtain an authentication token that will be used in all of the requests dedicated to do linguistics checks.
In this section we will be creating a class that does all the work for us. The basic skeleton of the class is shown in the listing below:
...
Codeblock | ||||||
---|---|---|---|---|---|---|
| ||||||
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace CongreeRestApiTutorial
{
class CongreeHttpClient
{
private string authUrl = "/congreeidentityserver/connect/token";
public CongreeHttpClient(string serverUrl)
{
this.authUrl = serverUrl + this.authUrl;
}
public string GetToken(string username, string password)
{
string token = "";
var client = new RestClient(this.authUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("authorization", "Bearer undefined");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&username=" + username + "&password=" + password + "&scope=webApi termTigerWebAppApi&client_id=CongreeWebAPI", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
AccessToken accessToken = new AccessToken();
accessToken = JsonConvert.DeserializeObject<AccessToken>(response.Content);
return accessToken.access_token;
}
}
}
|
...