Unable to log in

ssmaitra
I am unable to login using the sample code shared in the Python client documentation. My redirect URL is set to http://127.0.0.1

I tried firing the URL https://kite.zerodha.com/connect/login?api_key=my_api_key from my browser and get the reply
{
"error_type": "ForbiddenError",
"message": "Invalid api_key",
"status": "error"
}
Note: I have already double checked the api_key.

Please suggest the next course of action.
  • ssmaitra
    I am using requests library to make the query and following is the code snippet and its output.

    Code
    kite = KiteConnect(api_key=self.__api_key)

    # Redirect the user to the login url obtained
    # from kite.login_url(), and receive the request_token
    # from the registered redirect url after the login flow.
    # Once you have the request_token, obtain the access_token
    # as follows.

    print 'Login url: ', kite.login_url()
    response = requests.get(kite.login_url(), allow_redirects=False)
    print 'Status code: ', response.status_code
    print 'Headers: ', response.headers
    print 'History: ', response.history
    print 'Reason: ', response.raw.reason
    print 'Redrect url: ', response.headers['location']

    Output
    Login url: https://kite.trade/connect/login?api_key=09y9sc1765udoxpb
    Status code: 302
    Headers: CaseInsensitiveDict({'content-length': '154', 'server': 'nginx', 'connection': 'keep-alive', 'location': 'https://kite.zerodha.com/connect/login?api_key=09y9sc1765udoxpb', 'date': 'Wed, 06 Jul 2016 19:30:37 GMT', 'content-type': 'text/html'})
    History: []
    Reason: Moved Temporarily
    Redrect url: https://kite.zerodha.com/connect/login?api_key=09y9sc1765udoxpb

    If you notice, the redirect URL does not contain the request_token. Can you check where could be the problem ?
  • RN0316
    I am getting the same error.
  • Vivek
    @ssmaitra Your app 'CountXTreme' which you tried to create is in pending state, it might be because some error while creating the app or making payments. I checked payment history and there were no payments made yet so please kindly add credits and activate the app.
  • ssmaitra
    I am getting the error Insufficient balance. Please add funds to your Zerodha account. I have much more that the required 2000 credits in my Zerodha account. Can you check please?
  • Vivek
    @ssmaitra Your ledger balance is ₹1,998.65 hence we couldn't deduct.
  • ssmaitra
    I have paid using credit card and my app is active now. However, I am still facing the same issue. There is no request_token in the redirect url

    Login url: https://kite.trade/connect/login?api_key=09y9sc1765udoxpb
    Status code: 302
    Headers: CaseInsensitiveDict({'content-length': '154', 'server': 'nginx', 'connection': 'keep-alive', 'location': 'https://kite.zerodha.com/connect/login?api_key=09y9sc1765udoxpb', 'date': 'Thu, 07 Jul 2016 17:27:55 GMT', 'content-type': 'text/html'})
    History: []
    Reason: Moved Temporarily
    Redrect url: https://kite.zerodha.com/connect/login?api_key=09y9sc1765udoxpb
  • ssmaitra
    I tried firing the login url https://kite.trade/connect/login?api_key=09y9sc1765udoxpb from the browser and was directed to the login page. After logging in, I was able to see the request_token in the redirect url.

    How do I log in automatically without prompting for login page? BTW, I will be storing passwords in a secure password vault.
  • Vivek
    @ssmaitra User login flow is mandatory to obtain access_token and we can't provide full automation for that. You can check tool like selenium for that.
  • suraj
    Hi @ssmaitra You can use python mechanize library to automate your obtaining request token and generating access token business. Its quite simple and takes around 7-11 secs to generate an access token.
  • Akki
    Suraj can you paste sample code to get that?
  • Akki
    using python mechanize library obtaining request token and generating access token throws invalid request token.

    sampe code -
    kite = KiteConnect(api_key)
    #xyz = kite.login_url()
    #print "kite" ,kite
    #browser= mechanize.Browser()
    #browser.add_password('https://kite.trade/connect/login?api_key=xxxx', 'xxxx', 'xxxx')
    #response = browser.open('https://kite.trade/connect/login?api_key=xxxx')
    #xyz = response.geturl()
    #print xyz
    #request_token =xyz[47:79]
    #print request_token
  • neerajgoyal12
    I am able to successfully submit this form and get a response on redirect url using selenium and python, I have a problem though that it start asking to launch kite app on mobile and key in 6 digit OTP, for algo trading this wont be possible to do, can you suggest a way out.
  • neerajgoyal12
    Also, for retail algo traders is there a plan to disable this web browser dependency for login and make it pure rest api based with token refresh etc. ?
  • sujith
    @neerajgoyal12,
    It is mandatory by the exchange that a user has to login manually at least once a day. We don't recommend automating login.
This discussion has been closed.