Open Sajeyks opened 1 year ago
I configured everything as guided and it worked. Then I come the next day and rerun the code without changing anything and I get the:
{ "requestId":"5115-78674050-1", "errorCode": "404.001.03", "errorMessage": "Invalid Access Token" }
. So after waiting impatiently, it worked again. This keeps happening. Is there a possibility that it's lagging behind on updating theaccess token
, coz you said that it does that automaticaly?
@martinmogusu
Let me take a look...
I tried to reproduce your scenario, I see that it might be related to a previous issue in version 1.2.0 on access token expiry time calculation. This issue has been addressed in the latest version of the package (1.3.0)
Please run pip install django_daraja --upgrade
to get the latest version.
If your dependency version is in your requirements.txt
file, you can specify the version there as well.
Please let me know if this works for you.
hi i had the same problem gave me sleepless nights for days here is how i solved it : Use a middleware function to generate the token then call it just before the stkPush route e.g app.post("/stkpush",generateToken,(req,res){} and here is my source code const express = require("express"); const cors = require("cors"); const app = express(); const axios = require("axios"); const bodyParser = require("body-parser");
require("dotenv").config(); const PORT = process.env.PORT;
app.use(express.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cors());
const getAccessToken = async () => { const consumerKey = "wcMZ02TzOagGtiYg9oGRpfdR8BXYOFMN"; const consumerSecret = "Axvzm9tnBQxYcJ33";
try {
const url =
"https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials";
const encodedCredentials = Buffer.from(
${consumerKey}:${consumerSecret}
).toString("base64");
console.log(my credentials ${encodedCredentials}
);
const headers = {
Authorization: Basic ${encodedCredentials}
,
};
const response = await axios.get(url, { headers });
return response.data.access_token;
} catch (error) { throw new Error("Failed to get access token."); } };
// Middleware function to generate access token const generateToken = async (req, res, next) => { try { const token = await getAccessToken(); req.token = token; next(); } catch (error) { console.error(error); res.status(400).json({ error: error.message }); } };
app.get("/stk", (req, res) => { res.send( `
@xavieromondi I also solved mine the same way... But I appreciate the help 👍
I configured everything as guided and it worked. Then I come the next day and rerun the code without changing anything and I get the:
{ "requestId":"5115-78674050-1", "errorCode": "404.001.03", "errorMessage": "Invalid Access Token" }
. So after waiting impatiently, it worked again. This keeps happening. Is there a possibility that it's lagging behind on updating theaccess token
, coz you said that it does that automatically?