It is necessary to receive data from the browser, I mean the authorization code. I use the OAuth2 from Yahoo service. This code is displayed in the address bar of the browser after successful authorization, than redirect to redirect_uri=.... Process is used to start the browser, after that I try to listen on port XXXX with HttpListener, however even if I get some response, it is a meaningless set of characters . (UTF-8, etc. tried, the result is the same)
How can I get the authorization code from the browser after successful authorization?
Code with HttpListener
static void SniffPort()
{
byte[] input = new byte[] { 1 };
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
socket.Bind(new IPEndPoint(IPAddress.Broadcast, 5000));
socket.IOControl(IOControlCode.ReceiveAll, input, null);
byte[] buffer = new byte[4096];
Task.Factory.StartNew(() =>
{
while (true)
{
int len = socket.Receive(buffer);
if (len <= 40) continue;
string bin = Encoding.Default.GetString(buffer, 0, len);
Console.WriteLine(bin);
}
});
}
static HttpListener _httpListener = new HttpListener();
static void Main(string[] args)
{
Console.WriteLine("Starting server...");
_httpListener.Prefixes.Add("https://localhost:5000/");
_httpListener.Start();
Console.WriteLine("Server started.");
SniffPort();
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Users\User\Desktop\chrome-win\chrome.exe");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process p = new Process();
startInfo.Arguments = "--app=https://api.login.yahoo.com/oauth2/request_auth?client_id=CLIENT_ID&redirect_uri=https://localhost:5000/&response_type=code&language=en-us";
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
Also I had idea with the FiddlerCore, but after using it, you have to change the settings related to the proxy in inetcpl.cpl. It also doesn't work if the browser remembers the user and logs in immediately after starting the browser, FiddlerCore does not provide this information.
Code with FiddlerCore
public class HttpProxy : IDisposable
{
public HttpProxy()
{
Fiddler.FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
Fiddler.FiddlerApplication.Startup(5000, true, false);
}
void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
{
Console.WriteLine(String.Format("REQ: {0}", oSession.url));
}
public void Dispose()
{
Fiddler.FiddlerApplication.Shutdown();
}
}
static HttpListener _httpListener = new HttpListener();
static void Main(string[] args)
{
HttpProxy httpProxy = new HttpProxy();
//Launching the browser as in the code above.
}