Token is Invalid or has Expired

MadanGoyal
Hi. I am facing this issue since this morning only. During login sequence, I am successfully able to obtain the request token. But when I try to obtain access token through kite.generatesession(,), it throws an exception 'Token is Invalid or has Expired'.

I am working on two client ids. Surprisingly, the above problem is happening only in one client, not the other. Same program, same logic. I rechecked all parameters on my kite developer app profile like api secret, key, redirect URL = https://zerodha.com
Postback URL = https://howutrade.in/kitepostback

I tried revoking permission and authorising again. Tried rebooting the computer. Cleared all cache, cookies. Nothing works. I am able to logon to this client on in Kite3 on any web browser but on my app.

Can you please advise. Thanks.
  • MadanGoyal
    In the above post the command used to get access token is

    kite.generatesession(requesttoken,apisecret)

    Didn't display properly before in the post, hence reposting for clarity.
  • sujith
    sujith edited March 2018
    How are you parsing the request token, you should be doing a substring of the redirect URL? You should be reading the request token query param.
  • MadanGoyal
    MadanGoyal edited March 2018
    Yes. That's how I'm doing it.
    1. I store the redirect URL in a variable.
    2. I spilt and store the three key value pairs {Status, Action, Request_Token}
    3. Fetch value of Request_Token to obtain Access Token vide generatesession command.

    This logic is working fine with one client ID, but is failing on the other. Also, prior to this morning, this problem was not occurring. I have been using my app for nearly two months now without any login hassles. Also I'm using all new Kite3 api commands and methods. Even the login url I am using has an explicit v3 as mentioned in kite connect docs. I have not done any changes in my app ever since you launched version 3 and I incorporated them. Maybe it is a problem with that client ID. Because in the morning at about 5-6 am it was working fine. Suddenly after that this problem started. That too without doing any changes in my app.
  • rockiyogi
    In kite3 URL changes dynamically, position of request token changes...sometime it is at last position, sometime at different... so i've coded like this

    int token_url = getUrl.IndexOf("request_token");
    string req_token = getUrl.Substring(token_url + 14, 32); // length of token is 32

    User user = kite.GenerateSession(req_token, MySecret);
    Thread.Sleep(5000); // necessary, because sometimes elements doesn't get loaded
    MyAccessToken = user.AccessToken;
    MyPublicToken = user.PublicToken;
  • MadanGoyal
    I agree. In kite3 the query params keep changing. I'm also using similar coding pattern, but slightly longer. Yours is simpler.

    'Check if the redirect url contains any params
    Dim iqs As Int32 = currurl.IndexOf("?".ToCharArray())
    If (iqs >= 0) Then
    If (iqs < currurl.Length - 1) Then
    Dim querystring As String = currurl.Substring(iqs + 1)
    Dim parts As String() = querystring.Split("&")
    Dim UrlParams As New Specialized.NameValueCollection
    For Each item In parts
    Dim pair As String() = item.Split("=")
    UrlParams.Add(pair(0), pair(1))
    Next
    If UrlParams.HasKeys() Then
    Dim values As String()
    Dim LoginSuccess As Boolean = False
    For Each key In UrlParams.Keys
    values = UrlParams.GetValues(key)
    If key = "status" Then
    If values(0) = "success" Then LoginSuccess = True
    End If
    If key = "request_token" Then
    m_reqtoken = values(0)
    clearBrowserSession()
    Me.Close()
    End If
    Next
    End If
    End If

    I am able to get a request token with this code of length 32. But it is throwing an exception while fetching access token through kite.generatesession command in the case of ONE client but working on the OTHER. Strange...
  • sujith
    Can you private message the client id and api key for which you are facing the issue?
  • MadanGoyal
    @sujith I've tried everything. My app logic is working perfectly for one client ID and throwing this exception on the other ID. I am guessing you will have to check from backend if there is any problem. Do I need to write a specific mail to support regarding this?
  • sujith
    Everything looks fine from our end. Are you sure, you are using correct client id with correct api_key, you are not interchanging right?
  • MadanGoyal
    That's strange. Its started working now. I haven't changed a thing. I don't know what happened here. Anyways Thanks for your help @sujith
  • MadanGoyal
    you can close this thread if not required. Thanks.
This discussion has been closed.