public AbstractCommand(string action, TRequest request, TResponse response) { this.Id = RequestId.Get(); this.Token = RequestToken.Get(); this.Tries = 1u; this.Action = action; this.RequestArgs = request; this.ResponseResult = response; this.onSuccessCallback = null; this.onFailureCallback = null; this.Time = 0u; }
internal static async Task <Result> GetToken( string clientId, string clientSecret, string state, string redirectUri, string htmlResponseToShowInBrowser) { // Creates an HttpListener to listen for requests on that redirect URI. var http = new HttpListener(); http.Prefixes.Add(redirectUri); Debug("Listening on " + redirectUri); http.Start(); // Creates the OAuth 2.0 authorization request. RequestIdentity.Request(clientId, state, redirectUri); // Waits for the OAuth authorization response. var context = await http.GetContextAsync(); // Brings the Console to Focus. ConsoleHack.BringConsoleToFront(); // Sends an HTTP response to the browser. var response = context.Response; var buffer = System.Text.Encoding.UTF8.GetBytes(htmlResponseToShowInBrowser); response.ContentLength64 = buffer.Length; var responseOutput = response.OutputStream; Task responseTask = responseOutput.WriteAsync( buffer, 0, buffer.Length).ContinueWith((task) => { responseOutput.Close(); http.Stop(); Debug("HTTP server stopped."); }); // Checks for errors. if (context.Request.QueryString.Get("error") != null) { return(new Result() { Token = null, Error = string.Format("OAuth authorization error: {0}.", context.Request.QueryString.Get("error")) }); } if (context.Request.QueryString.Get("code") == null || context.Request.QueryString.Get("state") == null) { return(new Result() { Token = null, Error = "Malformed authorization response. " + context.Request.QueryString }); } // extracts the code var code = context.Request.QueryString.Get("code"); var incoming_state = context.Request.QueryString.Get("state"); // Compares the receieved state to the expected value, to ensure that // this app made the request which resulted in authorization. if (incoming_state != state) { return(new Result() { Token = null, Error = string.Format( "Received request with invalid state ({0})", incoming_state) }); } Debug("Authorization code: " + code); return(new Result() { Token = await RequestToken.Get(clientId, clientSecret, code, redirectUri, state), Error = null }); }