Invalid `api_key` or `access_token

sagar_yv
i am receiving ticks data as usual but when i am trying to place order receiving below error. but before 28-apr-2022 it was working fine.
{
status: 'error',
message: 'Invalid `api_key` or `access_token`.',
data: null,
error_type: 'InputException'
}

how i tried to resolve this .
followed all steps from https://stocksdeveloper.in/knowledgebase/kite-incorrect-api_key-or-access_token/
  • sagar_yv
    @sujith can u please help here
  • SRIJAN
    SRIJAN edited April 2022
    Are you passing variety in upper-case when placing order?? It should be small-case .
  • sagar_yv
    yes passing variety as "regular"

    apart from that
    exchange: "NSE"
    tradingsymbol: "SUNPHARMA"
    transaction_type: "BUY"
    quantity: 1
    product: "MIS"
    order_type: "MARKET"
    validity: "DAY"

    @SRIJAN :/
  • SRIJAN
    Then your access token expired. Try with a new access token.
  • sagar_yv
    tried multiple time as mentioned above from 28-apr-2022 i am facing this issue ticks are coming correctly but issue is from order placing side. i am running my service from past 1 month now (generated token every day manually around 9AM) . on 27-apr-2022 API has to expire so i renewed it on 25-apr-2022 . right after one month its happning so i generated new api_key secret still no success :disappointed:
    @SRIJAN
  • SRIJAN
    SRIJAN edited April 2022
    Are other APIs working fine except for order placement??
    And,do you start websocket every day or it's continuous??
  • sujith
    why don't you run kiteconnecjs in debug mode and paste the complete stack trace here?
  • sagar_yv
    i just use websockets for ticks and placeOrder API but just checked on getPositions is throwing same error just websockets ticks are working fine as of now.
    @SRIJAN
  • SRIJAN
    SRIJAN edited April 2022
    Are you using set_access_token method?
  • sagar_yv
    even if i restart service without changing access token then still websockets ticks working fine and i can see running sessions from my APP its showing my service active session.

    can u tell how can i get debug logs ?
    i have passed debug: true key in kiteConnect?
    @SRIJAN
  • sagar_yv
    if u are asking request response

    request
    {"url":"/portfolio/positions","method":"get","data":null,"headers":{"common":{"Accept":"application/json, text/plain, */*"},"delete":{},"get":{},"head":{},"post":{"Content-Type":"application/x-www-form-urlencoded"},"put":{"Content-Type":"application/x-www-form-urlencoded"},"patch":{"Content-Type":"application/x-www-form-urlencoded"},"X-Kite-Version":3,"User-Agent":"kiteconnectjs/3.2.1","Content-Type":"application/x-www-form-urlencoded"},"params":{},"baseURL":"https://api.kite.trade","transformRequest":[null],"transformResponse":[null],"timeout":7000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false}}

    response

    {status: 'error', message: 'Invalid api_key or access_token.', data: null, error_type: 'InputException'}


    if this is not required please let me know how could i help in this please

    @SRIJAN
  • SRIJAN
    SRIJAN edited April 2022
    This can only happen if you don't use set access token method.
    If you are using that,and still the error,I can't figure out the issue brother.
    Only Sujith Sir, Rakesh Sir can help you.
  • sagar_yv

    var ticker = new KiteTicker({
    api_key: apiKey,
    access_token: AccessToken,
    });

    ticker.autoReconnect(true, 10, 5);
    ticker.connect();
    ticker.on("ticks", TicksFunc);
    ticker.on("connect", subscribe);

    ticker.on("noreconnect", function () {
    logger.error("noreconnect");
    });

    ticker.on("reconnecting", function (reconnect_interval, reconnections) {});

    function subscribe() {
    ticker.subscribe(uniqueStocks);
    ticker.setMode(ticker.modeFull, uniqueStocks);
    }


    const options = {
    api_key: apiKey,
    debug: true,
    };

    const kc = new KiteConnect(options);
    kc.setAccessToken(AccessToken);


    then i use this kc to place order

    @SRIJAN
    and how and when can i connect to @sujith @rakeshr sir please help me .
  • rakeshr
    Can you paste here the complete debug log, by sending debug flag as true in the kiteconnect initialization, as shown here?
  • sagar_yv
    sagar_yv edited April 2022
    @rakeshr sir i have initialized kiteconnect as below

    const options = {
    api_key: apiKey,
    debug: true,
    };

    const kc = new KiteConnect(options);
    kc.setAccessToken(AccessToken);


    and get request response as below for placeOrder.
    if u want to see getHolding debug logs then i have put in above messages.

    request

    {"url":"/orders/regular","method":"post","data":"exchange=NSE&tradingsymbol=SUNPHARMA&transaction_type=BUY&quantity=1&product=MIS&order_type=MARKET&validity=DAY&variety=regular","headers":{"common":{"Accept":"application/json, text/plain, */*"},"delete":{},"get":{},"head":{},"post":{"Content-Type":"application/x-www-form-urlencoded"},"put":{"Content-Type":"application/x-www-form-urlencoded"},"patch":{"Content-Type":"application/x-www-form-urlencoded"},"X-Kite-Version":3,"User-Agent":"kiteconnectjs/3.2.1","Content-Type":"application/x-www-form-urlencoded"},"params":null,"baseURL":"https://api.kite.trade","transformRequest":[null],"transformResponse":[null],"timeout":7000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false}}

    response

    {status: 'error', message: 'Invalid `api_key` or `access_token`.', data: null, error_type: 'InputException'}

    if u wnt anything else please let me know
  • sujith
    This because your auth header doesn't have either api_key or access token as described in the error message.
  • sagar_yv
    how would i correct that ?

    i doing this :confused: . how could i resolve this. or can u share some curls so i will setup that only and will not use kite connect :disappointed:

    const options = {
    api_key: apiKey,
    debug: true,
    };

    const kc = new KiteConnect(options);
    kc.setAccessToken(AccessToken);

    @sujith
  • sagar_yv
    sagar_yv edited May 2022
    @sujith @rakeshr @SRIJAN i ssue is resolved thank you guys. :)

    its just somehow AccessToken is comming null here . i know its a silly one wil take care in future . thanks again. just wasted weekend over it .
This discussion has been closed.