From c901b619a4653729a43975927ce6ee4ac80f6407 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 25 Sep 2019 17:45:01 +0000 Subject: [PATCH] --- Controllers/AuthController.cs | 63 +++++++++++++++++++++++++++++++---- Startup.cs | 1 + 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index 9dbd9e2..37d76b5 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -3,7 +3,8 @@ using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; - +using System.Net.Http; +using Newtonsoft.Json.Linq; namespace qbridge.Controllers { @@ -12,10 +13,20 @@ namespace qbridge.Controllers [Produces("application/json")] public class OAuthRedirectController : ControllerBase { - + //used for discovery document + //https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.0 + private readonly IHttpClientFactory _clientFactory; + public JObject DiscoveryDoc { get; private set; } /* + + Discovery document + https://developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/openid-connect#discovery-document + Endpoints: + https://developer.api.intuit.com/.well-known/openid_sandbox_configuration + https://developer.api.intuit.com/.well-known/openid_configuration + QB Online developer account login creds for sandbox: login: support@ayanova.com @@ -38,9 +49,9 @@ namespace qbridge.Controllers sandbox company_us_1 sandbox-quickbooks.api.intuit.com */ - public OAuthRedirectController() + public OAuthRedirectController(IHttpClientFactory clientFactory) { - + _clientFactory = clientFactory; } // Redirect endpoint @@ -53,8 +64,9 @@ namespace qbridge.Controllers } [HttpGet("Start")] - public IActionResult Get() + public async Task GetAsync() { + await GetQBDiscoveryDocument(); string url = string.Empty; var queryParams = new Dictionary() { @@ -66,7 +78,7 @@ namespace qbridge.Controllers }; url = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString("https://appcenter.intuit.com/connect/oauth2", queryParams); - // return Content($"AuthUrl: {url}"); + // return Content($"AuthUrl: {url}"); return Redirect(url); //will ask for login creds and permission then will redirect back to the Get method above with: @@ -77,6 +89,45 @@ namespace qbridge.Controllers } + + + public async Task GetQBDiscoveryDocument() + { + var request = new HttpRequestMessage(HttpMethod.Get, + "https://developer.api.intuit.com/.well-known/openid_sandbox_configuration"); + request.Headers.Add("Accept", "application/json"); + request.Headers.Add("User-Agent", "AyaNova-QBridge"); + + var client = _clientFactory.CreateClient(); + + var response = await client.SendAsync(request); + + if (response.IsSuccessStatusCode) + { + string data = await response.Content.ReadAsStringAsync(); + DiscoveryDoc = JObject.Parse(data); + } + else + { + DiscoveryDoc = null; + } + return; + // string baseUrl = "https://developer.api.intuit.com/.well-known/openid_sandbox_configuration"; //The 'using' will help to prevent memory leaks. //Create a new instance of HttpClient + // using (System.Net.Http.HttpClient client = new HttpClient()) + + // //Setting up the response... + + // using (HttpResponseMessage res = await client.GetAsync(baseUrl)) + // using (HttpContent content = res.Content) + // { + // string data = await content.ReadAsStringAsync(); + // if (data != null) + // { + // Console.WriteLine(data); + // } + // } + } + /* Plan: Make a web APP and api that runs on our server and handles getting tokens from the QB Online oAuth2 endpoints diff --git a/Startup.cs b/Startup.cs index 4a00fca..e80a11e 100644 --- a/Startup.cs +++ b/Startup.cs @@ -25,6 +25,7 @@ namespace qbridge // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddHttpClient(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }