private void AuthorizeApp() { Uri baseUrl = new Uri("https://api.bimsync.com"); //Test for an internet connection if (!Services.CheckForInternetConnection()) { throw new Exception("Your computer seems to be currently offline. Please connect it to the internet and try again."); } //Get Revit windows handles revitWindow = Services.GetForegroundWindow(); m_Handler.RevitWindow = revitWindow; int portNumber = 63842; // Creates a redirect URI using an available port on the loopback address. redirectURI = string.Format("http://{0}:{1}/", IPAddress.Loopback, portNumber.ToString());// GetRandomUnusedPort()); m_Handler.RedirectURI = redirectURI; //grant permissions to the redirectURI, to run HttpListener in non-admin mode if (!Services.IsAdministrator()) { Services.AddAddress(redirectURI, Environment.UserDomainName, Environment.UserName); } // Creates an HttpListener to listen for requests on that redirect URI. if (http != null) { if (http.IsListening) { http.Stop(); } } http = new HttpListener(); http.Prefixes.Add(redirectURI); //Catch a specific error on the HttpListener //https://stackoverflow.com/questions/4019466/httplistener-access-denied try { http.Start(); } catch (HttpListenerException ex) { if (ex.ErrorCode == 183) { throw new Exception("Something went wrong with the login process. Please restart Revit before trying again."); } throw new Exception("The application can't login without admin access right. " + ex.Message); } if (!Services.CheckForPortAvailability(IPAddress.Loopback.ToString(), portNumber)) { http.Stop(); throw new Exception("It seems that the port N°63842 is closed or unavailable at the moment. " + "The URL " + redirectURI + " cannot be reached. " + "The application could not connect to the bimsync server. Contact your system administrator."); } // Creates the OAuth 2.0 authorization request. string authorizationRequest = string.Format("{0}/oauth2/authorize?response_type=code&redirect_uri={1}&client_id={2}&state={3}", baseUrl.OriginalString, System.Uri.EscapeDataString(redirectURI), bimsync.Services.client_id, "1"); // Opens request in the browser. Process browserProcess = Process.Start(authorizationRequest); //Get the browser in front if (browserProcess != null) { Services.SetForegroundWindow(browserProcess.MainWindowHandle); } m_Handler.Http = http; }