It looks like you're new here. If you want to get involved, click one of these buttons!
using System;
using KiteConnect;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Diagnostics;
namespace KiteConnectLoginExample
{
class Program
{
static void Main(string[] args)
{
login();
Console.ReadKey();
}
public static async void login()
{
Kite kite = new Kite("api_key", Debug: true);
// Generate a redirect url for local http server
string redirectURL = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort());
Console.WriteLine("Redirect URL: " + redirectURL);
// Generate kite login url with the above redirect url
string loginURL = string.Format("{0}&redirect_url={1}", kite.GetLoginURL(), redirectURL);
Console.WriteLine("Login URL: " + loginURL);
// Start local http server for catching the redirect url
var http = new HttpListener();
http.Prefixes.Add(redirectURL);
http.Start();
Console.WriteLine("Waiting for login to finish");
// Launch default browser with login url
Process.Start(new ProcessStartInfo(loginURL) { UseShellExecute = true });
// Wait until login is complete and redirect url is launched
var context = await http.GetContextAsync();
// Send a response back to browser
var response = context.Response;
string responseString = string.Format("Please return to the app");
var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
var responseOutput = response.OutputStream;
Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
{
responseOutput.Close();
http.Stop();
Console.WriteLine("HTTP server stopped.");
});
// Collect request token from the redirect url
var requestToken = context.Request.QueryString.Get("request_token");
if (requestToken == null)
{
Console.WriteLine("Login was not completed");
return;
}
// Use request token to generate access token
User user = kite.GenerateSession(requestToken, "app_secret");
// Store this locally and reuse it. This is valid for one day
Console.WriteLine("Access token: " + user.AccessToken);
kite.SetAccessToken(user.AccessToken);
kite.GetHoldings();
}
// Helper function to get a random local port to start an http server
public static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
}
thanks for the effort thou.
Thanks
Regards.
I am trying this, but the process stops after opening the browser. It says "Waiting for login to finish" and doesn't move ahead even after login is complete.
Can you suggest what could have been the issue?
PS: I was using default Webbrowser component but it has stopped working and hence trying above mentioned process.
Console.ReadKey();
I believe somewhere you might have pressed some key and that made the app close before it gets the redirect.This is just one example to do it. You might have to change the strategy to keep the app alive according to your app's behaviour.
Tried debugging it, code stops at "var context = await http.GetContextAsync();" line and it doesn't move forward.
Currently, I am manually taking request token after login and using it for app login.
After login one gets a request token in the redirect URL. It is not an access token.
Are you calling generate session with the request token?
Also let us know what are the modifications you did to your code. Above code should work fine just by replacing api key and secret. Also if you have any firewall running make sure it is not blocking your application.
I am using same code, without any modifications.
Console.WriteLine("Login URL: " + loginURL);
What might be appearing as stuck up, could actually be an error.
and your control could have moved passed too.
not the hard Exception type, but web operation related.
Try putting this operation under try{}catch( Exception ex )
and is this operation being run under full - administrative privileges.
try running the code as administrator.
sorry if you are doing it already.
Thanks
Regards
I have figured out the issues, somehow code was not going till Console.ReadKey(); line. Now it is working fine.
Thanks for all your support.
var context = await http.GetContextAsync();
i.e.
var context = Task.Run(async () => await http.GetContextAsync()).Result;