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;
}
}
}
|
The code above uses Congree authentication, i.e. users are manged inside of Congree. The recommended method, however, is to use TrustCMS. This method requires that a TrustCMS password was configured in Congree's Identity Server. Ask you Congree admin for the password.
The sample below shows how to use TrusCMS to get an access token.
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", "congree_auth_type=trustcms&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; } } } |
Hinweis |
---|
If you are using an empty password the value must be passed as single quotes: string password = "''"; |
...