Has the response string of request token. changed in login flow.

trade_then
trade_then edited March 2018 in .Net API client
I got this response while trying to login.

https://kite.trade/?request_token=28zcRP0wg80bQxqAfRD3OP2J9iv32uok&action=login&status=success

I want to clarify few doubts.
  1. Did &action=login part existed before also. or is it a new addition.
  2. If it existed earlier, has its placement within the response string changed.
  3. Is it in your control which parameter will be placed where or we will receive it at arbitrary locations.
    • i.e it could be sometimes &action=login&status=success
    • and other times &status=success&action=login
My app seems to be broken because of this today.

To me it seems something has changed. because the initial screen which appears for login in web view is more prominent than before. or maybe it could be because of the Microsoft updates on its .net components.

Thanks
Regards
  • trade_then
    trade_then edited March 2018
    okay So now here is another response
    1. Previously
    2. https://kite.trade/?request_token=28zcRP0wg80bQxqAfRD3OP2J9iv32uok&action=login&status=success
    3. Now
    4. https://kite.trade/?action=login&status=success&request_token=QAWOAzBo9CuS8S4Ms58WjdhsSaMDQnIQ
    So clearly response string is not placing parameters at same locations. Is this something new.
    I never had any issue with this before.

    Is this normal behaviour.

    Although I have fixed my app it is not dependent on parameter location anymore.

    Thanks
    Regards

  • Vivek
    @trade_then Action is a new query arg which we sent to identify the type of connect login attempted fox example login, basket etc. Your app should ideally be parsing the query args and check values for only the keys which you are interested in and shouldn't break when we add new key.
  • trade_then
    trade_then edited March 2018
    @vivek Thanks for the response.
    already fixed it on my side.
    Would you kindly explain my 3rd point in 1st question. I am curious!.

    As you can see in my second comment, 2 response strings have query parameters at completely different locations/indexes!.

    I have never programmed web app.

    Thanks
    Regards


  • Vivek
    Vivek edited March 2018
    If it existed earlier, has its placement within the response string changed.
    Order of keys in query params shouldn't matter. Any url parsing library will parse this and gives you a map so order doesn't matter.
    Is it in your control which parameter will be placed where or we will receive it at arbitrary locations.
    i.e it could be sometimes &action=login&status=success
    and other times &status=success&action=login
    As said above you should be parsing it with a url parser instead of doing a string match. Order shouldn't matter in that case.

    You can read about the spec here - https://tools.ietf.org/html/rfc3986#section-3.4
  • ZK2689
    @trade_then please share your fix, because I just found this and need to redo my login section of my app.

    Thanks & Regards.
  • ZK2689
    ZK2689 edited March 2018
    Currently I am using this fix:

    string toBeSearched = "request_token=";
    int ix = textBoxAddressBar.Text.IndexOf(toBeSearched);

    if (ix != -1)
    {
    string requestToken = textBoxAddressBar.Text.Substring(ix + toBeSearched.Length);

    //new code
    if (requestToken.Contains("&"))
    {
    requestToken = requestToken.Split('&')[0];
    }
    //rest of the code
    }

    Thanks & Regards.
  • trade_then
    @ZK2689

    private void Login_WebBrowser_Navigated( object sender, NavigationEventArgs e )
    {
    Title = e?.Uri?.ToString() ?? string.Empty ;
    Debug.WriteLine( Title ) ;

    var query_part = e?.Uri?.GetComponents( UriComponents.Query, UriFormat.UriEscaped ) ?? string.Empty ;

    if ( string.IsNullOrWhiteSpace( query_part ) ) return ;

    if ( query_part.Contains( "status".Trim() ) && query_part.Contains( "request_token".Trim() ) )
    {
    Debug.WriteLine( $"{query_part}" ) ;

    var query_splitted = query_part.Split( '&' ) ;

    int? success_part_idx = null ;
    int? request_token_part_idx = null ;

    for( int i = 0; i < query_splitted.Length; i++ )
    {
    if ( query_splitted[i].Contains( "success".Trim() ) )
    success_part_idx = i;
    if ( query_splitted[i].Contains( "request_token".Trim() ) )
    request_token_part_idx = i;

    if ( success_part_idx != null && request_token_part_idx != null )
    break ;
    }

    if ( success_part_idx == null || request_token_part_idx == null )
    return ;

    var request_token_params = query_splitted[ request_token_part_idx.Value ].Split( '=' ) ;

    Debug.WriteLine( $"{request_token_params[0]} = {request_token_params[1]}" ) ;

    var user_credentials = kite.GenerateSession( request_token_params[1], Secret_TextBox.Text.Trim() ) ;
    userInfo = user_credentials ;

    Store_LoginInfo( user_credentials ) ;

    }

    }
    Thanks
    Regards
  • ZK2689
    Hi @trade_then, today during the market data issue I had a network exception at kite.PlaceOrder which stated it took too long to respond and my app crashed, Please let me know how do you handle such exceptions?

    Thanks & Regards.
  • trade_then
    trade_then edited March 2018
    @ZK2689
    I have not dealt with such issues yet with PlaceOrder. Your PlaceOrder call will ultimately boil down to
    Request. function.

    now let us see where you might have got stuck!.
    this is where actual network call happens

    code there seems to be dealing with error significantly.
    what exactly did you receive as error? if it was a NetworkException then try putting your code in try Catch at your end and deal with exception as you deem fit according to your logic.

    As to what I do well my code if heavily
    • try-catched.
    • every aspect of app is segregated from one another as WCF services. so basically whole app is a collaboration of various apps communicating with each other.
    So one aspect of app cannot take down the whole application. the part which goes down can be started instantly without other-parts noticing the effects.


    here is a screen-shot of how divided the app is and this does not even cover algo and charting.



    Thanks
    Regards
  • tonystark
    @ZK2689 @trade_then You should never parse URLs manually. The standard library has built-in functions for that.

    For example:
    class Program
    {
    static void Main(string[] args)
    {
    Uri tmp = new Uri("http://www.google.co.uk/search?hl=en&q=parsing+a+url+in+c%23&aq=f&aqi=g1g-j9&aql=&oq=");

    Console.WriteLine("Protocol: {0}", tmp.Scheme);
    Console.WriteLine("Host: {0}", tmp.Host);
    Console.WriteLine("Path: {0}", HttpUtility.UrlDecode(tmp.AbsolutePath));
    Console.WriteLine("Query: {0}", tmp.Query);
    NameValueCollection Parms = HttpUtility.ParseQueryString(tmp.Query);
    Console.WriteLine("Parms: {0}", Parms.Count);
    foreach (string x in Parms.AllKeys)
    Console.WriteLine("\tParm: {0} = {1}", x, Parms[x]);

    Console.ReadLine();
    }
    }
    You will have to add a reference to System.Web.
  • trade_then
    @tonystark
    Thanks for the utility. never dealt with web before.
    so resorted to brute-force

    Thanks
    Regards
  • ZK2689
    @trade_then & @tonystark great help guys, I just now need to handle those odd exceptions, I don't know If the order was placed for that request or not but I will still update the code with a try catch block and see what happens next time if this happens, if now order gets placed i will just reorder it in the catch block.
  • ZK2689
    This was the exception I received:
    System.Net.WebException: 'The operation has timed out'
  • sujith
    Can you let us know for which API call you got this error and steps to reproduce this?
    It doesn't seem like an error from Kite Connect API.
  • ZK2689
    ZK2689 edited March 2018
    @sujith this caused the error:
    Dictionary sellResponse = kite.PlaceOrder(
    Exchange: "NSE",
    TradingSymbol: "ACC",
    TransactionType: Constants.TRANSACTION_TYPE_SELL,
    Quantity: 1,
    OrderType: Constants.ORDER_TYPE_MARKET,
    Product: Constants.PRODUCT_MIS
    );

    This works almost all the time but once out of blue it throws this exception. My app strategy works on multiple instruments at the same time(currently testing with 3) and a buy/sell signal is generated independently of each other which creates an event and an order is placed.
  • MAG
    @Z@ZK2689
    Since we connect to zerodha servers over the internet, it could be intermittent network errors at your end too or something as simple as dns lookup failure.
    Check your dns setting and if its your ISP's DNS servers, set it to google public dns server (8.8.8.8)
    Also check for packet loss at your end by doing a continuous ping test for an hour or two.
  • ZK2689
    @MAG This happened today too...this doesn't effects order placement, just the response is broken, I don't get the order id for these odd orders , but now since I have placed order placement under try catch, this time app didn't crashed. I am working to improve my app from here.

    Thanks & Regards.
  • MAG
    @ZK2689 Great.

    In parallel I would still suggest monitoring network connection on the side.

    I caught issues with one provider and switched to another when I found that the first provider used to intermittently drop packets for a few seconds at random intervals through the day. The second provider obviously costs more - almost double - but is worth the money because connection reliability is paramount.
Sign In or Register to comment.