-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
56 lines (44 loc) · 1.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Newtonsoft.Json;
using System.Net.Http.Headers;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapPost("/token", async (HttpRequest request) => {
string body = String.Empty;
using (StreamReader reader = new StreamReader(request.Body))
{
body = await reader.ReadToEndAsync();
}
Console.WriteLine("This is the body of the request: " + body);
var url = new Uri("https://identity.fortellis.io/oauth2/aus1p1ixy7YL8cMq02p7/v1/token");
var handler = new HttpClientHandler();
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Basic {yourAPIKey:APISecret}");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent data = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("redirect_uri", "http://localhost:{yourPort}/"),
new KeyValuePair<string, string>("code", body)
});
Console.WriteLine($"This is what's in the data: {data.ToString}");
var res = await client.PostAsync(url, data);
var content = await res.Content.ReadAsStringAsync();
Console.WriteLine("This is the content of the response: " + content);
var tokenResponse = JsonConvert.DeserializeObject(content);
Console.WriteLine("This is the token response parsed: " + tokenResponse);
return content;
});
app.Run();