usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
25.67k stars 1.17k forks source link

Initialisation Issue #2457

Open ajnayak123 opened 3 months ago

ajnayak123 commented 3 months ago

Error invoking remote method 'send-http-request': ReferenceError: bearerTokenVarName is not defined : This is the issue I am facing currently. I have defined bearerTokenVarName in the script and have also passed it as a parameter in No Bearer Auth.

Can anyone help me in this case?

fgrande commented 3 months ago

Can You share the bru files to rebuild this situation ?

ajnayak123 commented 3 months ago

const axios = require('axios'); const btoa = require('btoa'); const moment =require('moment'); const calculateTokenExpiryInMilliseconds = (tokenExpiryVarName) => { const currTime = moment() const expiryTime = moment(bru.getVar(tokenExpiryVarName)) || currTime return expiryTime.diff(currTime,'milliseconds') } console.log('check') const authenticateClient = async(config) => { const { tokenEndpointVarName, clientIDVarName, clientServerVarName, bearerTokenVarName } = config } console.log('bearerTokenVarNam1')

console.log('check:$(bearerTokenVarName)') const expiryDiffinMilliSec = calculateTokenExpiryInMilliseconds('bearerTokenVarName') const oneMininMilliseconds = 60000 if(expiryDiffinMilliSec >= oneMininMilliseconds && bru.getVar("RequestedEnvName") === bru.getEnvName()){ console.log('Taking existing token') return
}
console.log('Taking new Token') const envVar = {

[clientIDVarName]:bru.getEnvVar(clientIDVarName),
[clientServerVarName]:bru.getEnvVar(clientServerVarName)

} const missingVar = Object.entries(envVar).filter(([key, value]) => !value).map(([key])=> key) if(missingVar.length > 0){ throw new Error('Missing env Variables: ${missingVar.join(', ')}') } try { const tokenData = await axios.post(envVartokenEndpointVarName,{grant_type: "client_credentials"}, {headers: {"Authorization":OAUTH 2.0 ${btoa(${ envVar[clientIDVarName]}:${envVar[clientServerVarName]})}, "/content-Type": "application/x-www-form-urlencoded"}}).data bru.setVar(bearerTokenVarName,tokenData.accesstoken) bru.setVar('expiryFor${bearerTokenVarName}',moment().add(tokenData.expires_in,'seconds').format()) bru.setVar('RequestedEnvName',bru.getEnvName()) } catch(error){ throw error } await authenticateClient({ tokenEndpointVarName: 'tokenUrl', clientIDVarName: 'clientId', clientServerVarName: 'clientServer', bearerTokenVarName: 'bearerToken2' } )