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:
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; } } }
The constructor expects as single argument which is the base url of the server where Congree is installed, e.g. http://localhost.
Wihtin the constructor the argument is being concatenated with the contents of the class varibale authurl, to produce an URL like http://localhost/congreeidentityserver/connect/token.
In the next step we add a method that returns a authentication token. The method accepts to arguments: the user's username and his password. If the password is empty an empty string is being passed.
To allow for an easier access to the JSON object returned from the server the sample below uses a helper class "AccessToken" that is being used to deserialize the JSON object to a C# object.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CongreeRestApiTutorial { public class AccessToken { public string access_token { get; set; } public string expires_in { get; set; } public AccessToken() { } } }
The class shown above is being used as the target for the serialization of the JSON object using the tools from Newtonsoft.Json.
The code below uses RestSharp. You may want to add this package to your project as well.
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&client_id=CongreeWebAPI", ParameterType.RequestBody); IRestResponse response = client.Execute(request); AccessToken accessToken = new AccessToken(); accessToken = JsonConvert.DeserializeObject<AccessToken>(response.Content); return accessToken.access_token; } } }
If you are using an empty password the value must be passed as single quotes:
string password = "''";
The listing below shows how to use the class from within any other class:
using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace CongreeRestApiTutorial { class CongreeSampleApp { public CongreeSampleApp() { string username = "foo"; string password = "bar"; CongreeHttpClient congreeHttpClient = new CongreeHttpClient("http://localhost"); string token = congreeHttpClient.GetToken(username, password); } } }