Hi, I am unable to get the access token using python. I am using the API example to place an order, however, I am unable to get the access token. Any help is greatly appreciated. Thanks.
from kiteconnect import KiteConnect import hashlib import urllib2,requests,json
# Assuming you have obtained the `request_token` # after the auth flow redirect by redirecting the # user to kite.login_url() try: user = kite.request_access_token(request_token=request_token, secret=api_secret)
kite.set_access_token(user["access_token"]) except Exception as e: print "Authentication failed", str(e) raise
print user["xxxxxxxxx"], "has logged in"
# Get the list of positions. print kite.positions()
# Place an order. order_id = kite.order_place( tradingsymbol="INFY", exchange="NSE", quantity=1, transaction_type="BUY", order_type="MARKET" )
OUTPUT:
200 C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py:821: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning) Authentication failed Invalid session credentials Traceback (most recent call last): File "C:\Users\bhara\Desktop\kite2.py", line 37, in secret=api_secret) File "C:\Python27\lib\site-packages\kiteconnect\__init__.py", line 215, in request_access_token "checksum": checksum File "C:\Python27\lib\site-packages\kiteconnect\__init__.py", line 446, in _post return self._request(route, "POST", params) File "C:\Python27\lib\site-packages\kiteconnect\__init__.py", line 528, in _request raise(exp(data["message"], code=r.status_code)) kiteconnect.exceptions.TokenException: Invalid session credentials
You have to open the login url in browser to get the request token after you successfully authenticate. From your code I can see that your are just making a get request using urllib to login url and trying to get token but request token is issued once you successfully authenticate and it redirects to the app redirect url.
You can automate this with headless browsers such as mechanize or selenium.
You can automate this with headless browsers such as mechanize or selenium.