public oAuth2() { // Setup the Listener client responseListener = new LocalHttpListener(); if (!Timeout.HasValue) { Timeout = 60; } HasTimedout = false; }
public oAuth2(XeroConfiguration xeroConfig) { XeroConfig = xeroConfig; // Setup the Listener client responseListener = new LocalHttpListener(); if (!Timeout.HasValue) { Timeout = 60; } HasTimedout = false; }
// Because we need to launch a browser and wait for authentications this needs to be a task so it can wait. async Task BeginoAuth2Authentication() { if (string.IsNullOrEmpty(XeroConfig.ClientID)) { throw new ArgumentNullException("Missing Client ID"); } XeroConfig.ReturnedAccessCode = null; // Ensure the Return code cleared as we are authenticating and this propery will be monitored for the completion XeroConfig.XeroAPIToken = new XeroAccessToken(); // Reset this token as we are authenticating so its all going to be replaced //start webserver to listen for the callback responseListener = new LocalHttpListener(); responseListener.callBackUri = XeroConfig.CallbackUri; responseListener.config = XeroConfig; responseListener.StartWebServer(); //open web browser with the link generated System.Diagnostics.Process.Start(XeroConfig.AuthURL); // Basically wait for 60 Seconds (should be long enough, possibly not for first time if using 2FA) HasTimedout = false; int counter = 0; do { await Task.Delay(1000); // Wait 1 second - gives time for response back to listener counter++; } while (responseListener.config.ReturnedAccessCode == null && counter < Timeout); // Keep waiting until a code is returned or a timeout happens if (counter >= Timeout) { HasTimedout = true; } else { // Test if access was not granted // ReturnedAccessCode will be either a valid code or "ACCESS DENIED" if (responseListener.config.ReturnedAccessCode != XeroConstants.XERO_AUTH_ACCESS_DENIED) { this.ReturnedToken = XeroConfig.XeroAPIToken; ExchangeCodeForToken(); } } responseListener.StopWebServer(); }