I'm implementing an authorization process for using Imgur/Gfycat on my app using OAuth.
The idea is simple:
- Start HttpListener passing the expected callback URL.
- Open the auth URL in a browser.
- Upon receiving the callback, read the URL and get the tokens.
The issue that I'm facing is that when the app is already authorized (it may have been authorized in another machine, for example), the HttpListener returns the URL with just the state, without any tokens.
http://localhost/5556/?state=mystate
A few milliseconds later, I can see in the browser the whole URL, with tokens and all.
http://localhost/5556/?state=mystate&access_token=...
Adding two context = await _listener.GetContextAsync(); calls one after the other is not helpful, as it looks like the call blocks navigation.
_listener = new HttpListener();
_listener.Prefixes.Add(callbackUrl);
_listener.Start();
//Open browser passing auth url
Process.Start(authUrl);
var context = await _listener.GetContextAsync();
if (context.Request.QueryString.Count == 1)
context = await _listener.GetContextAsync();
//Interpret response...
How to solve this issue?