Uncaught InputException (400) 'Invalid `api_key` or `access_token`

I'm trying to get quotes for some tickers but the error says access_token is invalid. The API verison is 3.0.2b. Here's what I'm doing.

Login file
try {
$user = $kite->generateSession($_GET['request_token'], KITE_SECRET);

echo "Authentication successful. \n";

$connect = mysqli_connect($host, $username, $password, 'zerodha');
$saveAccessTokenQ = "UPDATE zerodha_config SET _value = '".$user->access_token."' WHERE _key = 'access_token'; UPDATE zerodha_config SET _value = '".mysqli_real_escape_string($connect, $_GET['request_token'])."' WHERE _key = 'request_token';";
$saveAccessToken = mysqli_multi_query($connect, $saveAccessTokenQ); //save access token to DB

} catch(Exception $e) {
echo "Authentication failed: ".$e->getMessage();
throw $e;
} else {
header('location: https://kite.trade/connect/login?v=3&api_key='.KITE_API);
Get quote file
$connect = mysqli_connect($host, $username, $password, 'zerodha');
$loadConfig = mysqli_query($connect, "SELECT _key, _value FROM zerodha_config");
while ($row = mysqli_fetch_assoc($loadConfig)) {
$config[$row['_key']] = $row['_value']; //access token loaded from DB

$kite = new KiteConnect(KITE_API);
$kite->setAccessToken = $config['access_token'];

try {
$quotes = $kite->getQuote($config['tickers']); //Error on this line
} catch(Exception $e) {
throw $e;
// requestLogin();
So first I visit the login page and login to Kite. This generates an access token and saves it in DB. Then I navigate to the second file and the error comes up:
Fatal error: Uncaught InputException (400) 'Invalid `api_key` or `access_token`.' thrown in /home/webserver/public_html/zerodha/phpkiteconnect-kite3/kiteconnect.php on line 899

What am I doing wrong?
  • sujith
    Can you check if you are sending api_key and access token properly while calling the API?
    You may enable debug logs and check.
  • VipulK
    Yes. I hard coded them in just to be sure and I still get the error.
  • VipulK
    If I put my quotes line in the login page itself, it works. There's something not right about saving and re assigning the access token. Can you help?
  • Imran
    hii @VipulK
    for the access token see this video
  • VipulK
    Ah I'm an idiot. It was a syntax error. Access token needs to be set like this
    Not this
    $kite->setAccessToken = $config['access_token'];

    Problem solved.
This discussion has been closed.