public static async Task Authorize() { var state = Guid.NewGuid().ToString("N"); var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Code, App.AppKey, App.RedirectUri); var http = new HttpListener(); http.Prefixes.Add(App.RedirectUri.OriginalString); http.Start(); // Launch the URI var success = await Windows.System.Launcher.LaunchUriAsync(authorizeUri); if (success) { await HandleOAuth2Redirect(http); var result = await HandleJSRedirect(http); if (result.State != state) { return; } App.AccessToken = result.AccessToken; } else { // URI launch failed } } private static async Task HandleOAuth2Redirect(HttpListener http) { var context = await http.GetContextAsync(); //while (context.Request.Url.AbsolutePath != App.RedirectUri.AbsolutePath) // await HandleJSRedirect(http); // We only care about request to RedirectUri endpoint. while (context.Request.Url.AbsolutePath != App.RedirectUri.AbsolutePath) { context = await http.GetContextAsync(); } // Respond with a HTML page which runs JS to send URl fragment. context.Response.ContentType = "text/html"; // Respond with a page which runs JS and sends URL fragment as query string // to TokenRedirectUri. using (var file = System.IO.File.OpenRead("index.html")) { file.CopyTo(context.Response.OutputStream); } context.Response.OutputStream.Close(); } private static async Task HandleJSRedirect(HttpListener http) { var context = await http.GetContextAsync(); // We only care about request to TokenRedirectUri endpoint. while (context.Request.Url.AbsolutePath != App.RedirectUri.AbsolutePath) { context = await http.GetContextAsync(); } var redirectUri = new Uri(context.Request.QueryString["url_with_fragment"]); var result = DropboxOAuth2Helper.ParseTokenFragment(redirectUri); return result; }