string Approval(string referanceKey, string confirmationCode, string tenantIdentifierNumber)
{
string json = "{"
+ "\"referanceKey\": \"" + referanceKey + "\","
+ "\"confirmationCode\": \"" + confirmationCode + "\","
+ "\"tenantIdentifierNumber\": \"" + tenantIdentifierNumber + "\","
+ "\"}";
return json;
}
string CreateToken1(string username, string password, string url)
{
var client = new RestClient(url);
client.Timeout = 5000;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("username", username);
request.AddParameter("password", password);
request.AddParameter("grant_type", "password");
IRestResponse response = client.Execute(request);
string access_token = string.Empty;
if (response.Content != string.Empty)
{
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(response.Content);
access_token = dict["access_token"];
}
return access_token;
}
string CreateToken2(string username, string password, string url)
{
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";
req.Accept = "application/json";
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password)));
byte[] formData = UTF8Encoding.UTF8.GetBytes("grant_type=password" + "&username=" + username + "&password=" + password);
req.ContentLength = formData.Length;
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
dynamic j = JsonConvert.DeserializeObject(result);
string accessToken = j.access_token;
return accessToken;
}
string CreateToken3(string username, string password, string url)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
HttpResponseMessage response = client.PostAsync("Token",
new StringContent(string.Format("grant_type=password&username={0}&password={1}"
, HttpUtility.UrlEncode(username), HttpUtility.UrlEncode(password))
, Encoding.UTF8, "application/x-www-form-urlencoded")).Result;
string resultJSON = response.Content.ReadAsStringAsync().Result;
string access_token = string.Empty;
if (resultJSON != string.Empty)
{
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(resultJSON);
access_token = dict["access_token"];
}
return access_token;
}
async Task<string> CreateToken4(string username, string password, string url)
{
HttpClient client = new HttpClient();
client.Timeout = new TimeSpan(0,0,5000);
client.DefaultRequestHeaders.Add("Authorization", "Basic "
+ Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes($"{username}:{password}")));
List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("grant_type", "password"));
postData.Add(new KeyValuePair<string, string>("username", username));
postData.Add(new KeyValuePair<string, string>("password", password));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(url));
request.Content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = client.PostAsync(url, new FormUrlEncodedContent(postData)).Result;
var token = await response.Content.ReadAsStringAsync();
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(token);
return dict["access_token"];
}
string UseToken1(string url, string json, string token)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
if (token != null)
{
httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
}
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
async Task<string> UseToken2(string url, string json, string token)
{
string result = string.Empty;
HttpClient client = new HttpClient();
if (token != null)
{
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
}
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Uri.EscapeUriString(url));
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage tokenResponse = client.PostAsync(url, request.Content).Result;
if (tokenResponse.IsSuccessStatusCode)
{
var token1 = await tokenResponse.Content.ReadAsStringAsync();
dynamic j = JsonConvert.DeserializeObject<dynamic>(token1);
result = j.data.referanceKey;
}
return result;
}
Task<string> getResponse = UseToken2("111", "222", "333");
ltResult.Text = getResponse.Result;