Generate IBM API Connect OAuth2 token programatically in Postaman

Hello everyone,

To run my whole collection, I need to generate an OAuth2 token. Because I want to run the collection by just pressing Run, and do not want to bother with generating a token manually for each folder, I have chosen to do this with a pre-request script. Everything is running like a charm, I am getting the Oauth2 token and when I run the tests one by one manually, the token is saved as an environment variable for the next requests to access and use it. That is great, BUT when I run the whole collections, sometimes it will seem that the variable is not being saved / or the following requests are not accessing it. Using setTimeout() seems to be working most of the time, but not always.

Have you experienced such a behavior? Am I missing something?

Thanks in advance!

Hi @tmladenov and welcome to the Postman community! :smiley:

Could you share the pre-request script you are using to set the OAuth token as an environment variable? Make sure to remove any sensitive information before.

Hello arlemi,

Sure thing, thank you for the warming welcome and quick reply!

Here’s the gist to the code:

I have a few remarks:

  1. The query is formulated to be accepted by IBM’s Oauth2 token generator API. You might want to change it to the one you’d be using;
  2. You need to come up with some scope;

After you get any kind of reply from an Oauth2 generating API, save it as a variable, then try to access that variable from a couple more scripts in the collection and send it somewhere. It won’t be available always. But this is specific to the Oauth2 generator I’m using…

Thanks in advance!

I wanted to try and replicate the same using API Connect but it doesn’t let me create an account using a email. :sweat_smile:

Is the “Access token missing from response.” error raised in the case where the token isn’t set? Or is the token returned correctly to your function but not set in Postman?
First case scenario you could play with postman.setNextRequest() to divert the flow when the API Connect endpoint doesn’t return what you need (see more in Building Workflows).

No problems. I think it might be partially due to the Oauth2 API. Most of the time the first script generates the token, the second script uses it, but the third script throws an error saying the client isn’t authorised. The funny thing is that yesterday I used setTimeout() to slow the execution down of each collection test, as the the API might have been thinking that the token is used simultaneously, and it worked. Today it didn’t work. :grinning:

I’ve tried so many variants, but I narrowed it down to using setTimeout() with 300ms delay in the pre-script that executes the above given code that gets the token. Thus, it appears the problem is in the API having some seconds time limit between the uses of each token, and not with Postman itself.

I use the script in almost the same scenario but with different scope and it works like a charm each time. I will make sure I haven’t made a mistake in the problematic collection and will let you know if everything is working correctly and how exactly is this issue solved. Might be useful for people generating tokens programmatically in Postman.

Hi. The problem is fixed. It appears a little bit of a break is needed between the requests in Postman after making a call to IBM’s Oauth2 token generator API and then between the use of the token in each request. So in every pre-script in the collection I’ve used setTimeout() with some delay before running the test script. Everything is working fine now. Postman is saving the values as expected. It was the API’s fault.

Thanks for the quick help. Very much appreciated!

PS: Using Postman since like a week. I like it. Keep up the good work! :slight_smile:

Glad you got it working and thanks for the kind words! We’ll do our best to keep it up. :rocket: