Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialisation Issue #2457

Open
ajnayak123 opened this issue Jun 13, 2024 · 2 comments
Open

Initialisation Issue #2457

ajnayak123 opened this issue Jun 13, 2024 · 2 comments

Comments

@ajnayak123
Copy link

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
Copy link
Contributor

fgrande commented Jun 13, 2024

Can You share the bru files to rebuild this situation ?

@ajnayak123
Copy link
Author

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 = {
[tokenEndpointVarName]:bru.getEnvVar(tokenEndpointVarName),
[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(envVar[tokenEndpointVarName],{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.access_token)
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'
}
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants