Getting Invalid API or Access token while placing order

shailesh
Hi Team,
I'm getting Invalid `api_key` or `access_token`. while placing market order. I'm getting access token from php script & stored that in database. while live market I'm using same access token to connect from database. Getting all live ticks data properly , but while placing order, I'm facing problem.

Please help me in this to resolve the issue.

  • sujith
    @shailesh,
    Are you sure you didn't call generate access token again?

    Websockets API only authenticates while connecting and not while sending every tick. So it is possible that your session has expired and you are still getting ticks.
  • shailesh
    No. when i generate the access token, it gets store in db & im fetching same access token from db while making connnection.
  • rakeshr
    @shailesh
    Can you paste here, complete param you are sending for order placement?
  • shailesh
    shailesh edited January 2020
    @rakeshr
    var KiteConnect = require("kiteconnect").KiteConnect;

    var api_key = "*******",
    secret = "********",
    request_token = "********",
    access_token = "*********";

    var options = {
    "api_key": api_key,
    "debug": true
    };


    kc = new KiteConnect(options);
    kc.setSessionExpiryHook(sessionHook);

    if(!access_token) {
    kc.generateSession(request_token, secret)
    .then(function(response) {
    console.log("Response", response);
    init();
    })
    .catch(function(err) {
    console.log(err);
    })
    } else {
    kc.setAccessToken(access_token);
    init();
    }

    function init() {
    console.log(kc.getLoginURL())

    regularOrderPlace("regular");
    invalidateAccessToken()
    }

    function sessionHook() {
    console.log("User loggedout");
    }

    function invalidateAccessToken(access_token) {
    kc.invalidateAccessToken(access_token)
    .then(function(response) {
    console.log(response);
    testOrders();
    }).catch(function(err) {
    console.log(err.response);
    });
    }

    function regularOrderPlace(variety) {
    kc.placeOrder(variety, {
    "exchange": "NSE",
    "tradingsymbol": "RELIANCE",
    "transaction_type": "BUY",
    "quantity": 1,
    "product": "MIS",
    "order_type": "MARKET"
    }).then(function(resp) {
    console.log(resp);
    }).catch(function(err) {
    console.log(err);
    });
    }


  • shailesh
    Team,

    Any updates on above issue ?
Sign In or Register to comment.