Smart API Access token is getting expired when logged into angelbroking.com


  • Hi Team,

    I am using smart API to generate a session, and I am storing the access token in a file to avoid Password authentication each time. Whenever I need access smartapi, I am creating a object and set the jwt token and refresh token. Then I use that object to preform other calls. This will reduce authentication to only once per day.

    #My Snippet 
    from smartapi import SmartConnect
    
    ab=SmartConnect(api_key="DhpbUz6y")
    
    jwtToken    ='----My Token-----'
    refreshToken='----My Token-----'
    
    ab.setAccessToken(jwtToken)
    ab.setRefreshToken(refreshToken)
    
    print(ab.holding())
    

    However, when I log into angelbroking.com after generating the token, the token is getting expired since the authentication system apparently generates a new token.

    How can aviod such a situation when I need to use Broker GUI for other activities and API access for placing trades.

    vagrant@ubuntulab:/opt/kite/pykite$ p3 /opt/kite/pykite/ab/test2.py
    {'status': False, 'message': 'Invalid Session or Session is Expired Please Re-login', 'errorcode': 'AB1010', 'data': None}
    

    I tested using the trder-login instead of publisher-login in the login URL, but the result is same.

    class SmartConnect(object):
        #_rootUrl = "https://openapisuat.angelbroking.com"
        _rootUrl="https://apiconnect.angelbroking.com" #prod endpoint
        _login_url="https://smartapi.angelbroking.com/publisher-login" #prod endpoint
        _login_url="https://smartapi.angelbroking.com/trader-login" #Tested with this url too, result is same
    
    

    As a next option, I generated a authentication token with the below URL, But I cannot convert the Auth token into jwt token. I tried the generateToken(auth_token) method, but getting error message.

    https://smartapi.angelbroking.com/publisher-login?api_key=--My API Key--
    
    ## Error when used authtoken to generate refresh token generateToken(auth_token)
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/smartapi/smartConnect.py", line 195, in _request
        data = json.loads(r.content.decode("utf8"))
      File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/opt/kite/pykite/ab/test2.py", line 17, in <module>
        print(ab.generateToken(refreshToken))
      File "/usr/local/lib/python3.6/dist-packages/smartapi/smartConnect.py", line 257, in generateToken
        response=self._postRequest('api.token',{"refreshToken":refresh_token})
      File "/usr/local/lib/python3.6/dist-packages/smartapi/smartConnect.py", line 227, in _postRequest
        return self._request(route, "POST", params)
      File "/usr/local/lib/python3.6/dist-packages/smartapi/smartConnect.py", line 199, in _request
        content=r.content))
    smartapi.smartExceptions.DataException: Couldn't parse the JSON response received from the server: b''
    

    @admin @bhaveshjain @Bhaveshsjain


  • Hi @vnshsingh yes you can generate multiple session.


  • @admin I know that this is old post but need to get confirmation. Is multiple session LIVE as of today?


  • @admin That is a great news thanks.!


  • Hi @dheepi @prateekjjw001 @vishant , expected go-live date is 8th Jan.


  • @admin @bhaveshjain , Any update would be appreciated


  • @admin hi - do you have any ETA when this feature will be enabled? It is kind of a blocker. I assume lot of users would like to monitor their positions time to time even if using API for placing trades.


  • @admin ,
    So, then the solution is highly unusable. How could you expect us to do trading with only one session. For Eg: I need to place the order(s) and manage them using SmartApi, but I still need to check for other potential trades, scan through them using the broking GUI.

    Another example, my holding PNL in the api result is different from what is shown in the broker GUI. its not even close differing by 2 digits. This is where I need to cross verify between API results and the broker GUI.

    I understand that smartApi is in beta, but still the usability is completely lost. Sorry.

    I would highly recommend to implement this feature ASAP else the entire point of having API trading will be lost.


  • @vishant said in Smart API Access token is getting expired when logged into angelbroking.com:

    Can we expect multiple sessions (i.e mobile and web and SmartAPIs) in the near future?

    Hi @vishant , yes we are working towards enabling multiple session for our SmartAPI users as a part of our phase 2 development initiatives.


  • @admin : Can we expect multiple sessions (i.e mobile and web and SmartAPIs) in the near future?


  • @dheepi said in Smart API Access token is getting expired when logged into angelbroking.com:

    However, when I log into angelbroking.com after generating the token, the token is getting expired since the authentication system apparently generates a new token.

    Hi @dheepi , currently our systems support only one session at a time (either mobile or web or SmartAPIs).