Postman automated pre request for o Auth 2, not working properly

Viewed 26

So i want to execute my Postman projekt automated in CI/CD with newman i have found a pre request script but its not working for me properly

here the script

const tokenUrl = pm.environment.get("baseURL")+'/authorizationserver/oauth/token';

const clientId = pm.collectionVariables.get("client_id");
const clientSecret = pm.collectionVariables.get("client_secret");

const getTokenRequest = {
  method: 'POST',
  url: tokenUrl,
  body: {
      mode: 'formdata',
      formdata: [
          { key: 'grant_type', value: 'client_credentials' },
          { key: 'client_id', value: pm.collectionVariables.get("client_id") },
          { key: 'client_secret', value: pm.collectionVariables.get("client_secret") }
      ]
  }
};

pm.sendRequest(getTokenRequest, (err, response) => {
  const jsonResponse = response.json();
  const newAccessToken = jsonResponse.access_token;

  pm.variables.set('access_token_APP', newAccessToken);
});

so basically its a ouath 2 request just on scripting side,

Every time when i execute a request i want this pre request script to generate me a token, i store this generated token on folder level as a barrear token , and set authentification for the request for inherit auth by parent.

there is no value generated i already checked it with logging for the acces Token,

Do you see what i'm doing wrong , i can't see it

Thank you so much in advance

0 Answers
Related