Hi @yashwant.05rejtkv
It is expected behaviour that you will get 308 status. As it confirms that your app is authorised. It is returning the redirect URL and you have to handle this.
And for axios , you should write in try , catch block.
However I am providing to you to code which will help you to generate AuthCode
import otplib from 'otplib';
import axios from 'axios';
const BASE_URL = "https://api-t2.fyers.in/vagator/v2"
const BASE_URL_2 = "https://api.fyers.in/api/v2"
const URL_SEND_LOGIN_OTP = BASE_URL + "/send_login_otp"
const URL_VERIFY_TOTP = BASE_URL + "/verify_otp"
const URL_VERIFY_PIN = BASE_URL + "/verify_pin"
const URL_TOKEN = BASE_URL_2 + "/token"
const URL_VALIDATE_AUTH_CODE = BASE_URL_2 + "/validate-authcode"
const SUCCESS = 1
const ERROR = -1
async function sendLoginOTP(fy_id, app_id) {
try {
const response = await axios.post(URL_SEND_LOGIN_OTP, { fy_id, app_id });
if (response.status !== 200) {
return [ERROR, response.data];
}
const { request_key } = response.data;
return [SUCCESS, request_key];
} catch (error) {
return [ERROR, error.message];
}
}
async function verifyTOTP(request_key, totp) {
try {
const response = await axios.post(URL_VERIFY_TOTP, { request_key, otp: totp });
if (response.status !== 200) {
return [ERROR, response.data];
}
const { request_key: newRequestKey } = response.data;
return [SUCCESS, newRequestKey];
} catch (error) {
return [ERROR, error.message];
}
}
async function verifyPIN(request_key_2, PIN) {
try {
const ses = axios.create();
const payload_pin = {
"request_key": request_key_2,
"identity_type": "pin",
"identifier": PIN,
"recaptcha_token": ""
};
const res_pin = await ses.post(URL_VERIFY_PIN, payload_pin);
return [SUCCESS, res_pin.data];
} catch (error) {
return [ERROR, error.message];
}
}
async function GenAuthcode(FY_ID, TOTP_KEY, PIN, APP_ID, REDIRECT_URI,APP_TYPE) {
const APP_ID_TYPE = "2"
const send_otp_result = await sendLoginOTP(FY_ID, APP_ID_TYPE);
if (send_otp_result[0] !== SUCCESS) {
console.log(`send_login_otp failure - ${send_otp_result[1]}`);
process.exit(1);
} else {
console.log("send_login_otp success");
}
let verify_totp_result;
for (let i = 1; i setTimeout(resolve, 1000)); // Wait for 1 second before retrying
} else {
break;
}
}
const request_key_2 = verify_totp_result[1];
const ses = axios.create();
const payload_pin = {
"request_key": request_key_2,
"identity_type": "pin",
"identifier": PIN,
"recaptcha_token": ""
};
const res_pin = await ses.post(URL_VERIFY_PIN, payload_pin);
const ses1 = axios.create({
headers: {
'authorization': `Bearer ${res_pin.data['data']['access_token']}`
},
maxRedirects: 10,
});
const authParam = {
"fyers_id": FY_ID,
"app_id": APP_ID,
"redirect_uri": REDIRECT_URI,
"appType": APP_TYPE,
"code_challenge": "",
"state": "None",
"scope": "",
"nonce": "",
"response_type": "code",
"create_cookie": true
};
let authres
try {
authres = await ses1.post('https://api.fyers.in/api/v2/token', authParam);
} catch (error) {
if(error.response.status==308){
authres = error.response
}else{
console.log("Manual login requried for authorizing the app")
process.exit(0)
}
}
const url = authres.data['Url'];
const parsed = new URL(url);
const auth_code = parsed.searchParams.get('auth_code');
return auth_code;
}
var resp=await GenAuthcode("FYID","TOTPKEY","PIN","APPID without app type(without-100)","RedirectURL","apptype ie(100,102)")
var token=resp
console.log(token)