Need a Node + Express js code sample to get and access token

malipratik533
This is my code

___________________________________________________________________________________________________________________________________________________________________________
const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const axios = require('axios');

const app = express();
const port = 3000;

app.use(bodyParser.urlencoded({ extended: true }));

// Replace 'your_api_key' and 'your_api_secret' with your actual Zerodha API key and API secret
const yourApiKey = '--------------------------------';
const yourApiSecret = '---------------------------------';

// Step 1: Endpoint to initiate the login process
app.get('/login', (req, res) => {
const { api_key, redirect_url } = req.query;

// Redirect the user to the Kite Connect login page
const loginUrl = `https://kite.zerodha.com/connect/login?v=3&api_key=${api_key}&redirect_url=${redirect_url}`;
res.redirect(loginUrl);
});

// Step 2-4: Endpoint to handle the callback and obtain the access_token
app.all('/callback', async (req, res) => {
const request_token = req.query.request_token;
console.log('request_token', request_token);

// Step 2: Generate the checksum (SHA-256 of api_key + request_token + api_secret)
const checksumData = `${yourApiKey}${request_token}${yourApiSecret}`;
const checksum = crypto.createHash('sha256').update(checksumData).digest('hex');
console.log('checksum', checksum);

// Step 3: POST the request_token and checksum to /session/token
const tokenApiUrl = "https://api.kite.trade/session/token";
const payload = {
api_key: yourApiKey,
request_token: request_token,
checksum: checksum
};

try {
// Make a POST request to Zerodha API using axios
const response = await axios.post(tokenApiUrl, payload);

// Step 4: Obtain the access_token
const access_token = response.data.data.access_token;
console.log(`Access Token: ${access_token}`);
res.send(`Access Token: ${access_token}`);
} catch (error) {
console.error(`Error obtaining access token. Status Code: ${error.response.status}`);
console.error("Response:", error.response.data);
res.status(error.response.status).send("Error obtaining access token");
}
});

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});


___________________________________________________________________________________________________________________________________________________________________________
and error is
___________________________________________________________________________________________________________________________________________________________________________
Server is running on http://localhost:3000
request_token qRf9lePkIBKqUrEhmRlzR0RWZe7WhoX5
checksum f9490b31fab967ce86b251dffafcf1f86e75c9a5eb6e1428727746729c68bc92
Error obtaining access token. Status Code: 400
Response: {
status: 'error',
message: '`api_key` should be minimum 6 characters in length.',
data: null,
error_type: 'InputException'
}
___________________________________________________________________________________________________________________________________________________________________________
Sign In or Register to comment.