Updating token in collection

HI i have a question

in my Postman collection first API is to get token, then whole collection requires that authentication token to move forward, i wants to build a pipeline with jenkins/teamcity, but i am confused once i upload my collections to Github how it will over right the Token every-time i am running my build

is there any workaround for this ?

Hi @romy21romy and welcome here :slightly_smiling_face:

You may need to explain a bit more, is your issue with the fact that a new token will be generated every time you run your collection? In that case would you mind explaining why this is an issue?

If your question is about how to set a token and pass it forward for the other requests, you can use Environment Variables, as explained in that blog:

well my issue is i want to upload my both JSON (Collection and Environment) files to Github.
and from there i wants to call those files for my CI tool (jenkins or teamcity) using newman.

its not happening properly :frowning:

Hi @romy21romy,

This seems like you need to configure your CI tool to store the token as an environment variable, and then pull down that token each time you need to use it. This way you don’t persist the token to Github.

In terms of calling the collection with the environment, you should be able to do that with Postman API. Whatever you persist to Github will match what you already have.

You should be able to call the Postman API as well via newman.

Refer to the documentation below for how to use newman and Postman API.

https://learning.getpostman.com/docs/postman/collection-runs/command-line-integration-with-newman/

https://learning.getpostman.com/docs/postman/postman-api/intro-api/

If you would like a video to help explain, you can refer to these videos I made:

Newman

Postman API

In your collection, you can persist the token as an environment variable in a test script (using the test script), and then from there, continue using the token as a variable.

I hope this helps!
Orest

I use the same flow

  1. First request logs in using a grant_type = password and saves the access_token into a collection variable - pm.collectionVariables.set(“AccessToken”,data.access_token);
  2. The Collection is set to use Authorisation = oAuth2.0 and the AccessToken is set to {{AccessToken}}
  3. Subsequent requests have authorisation set to inherit from parent
  4. Collection works as expected

The problem is that step 2 is lost in the following scenarios

  1. Some time later or after another team member has used collection or postman has signed in again (unclear)
    2.Export the collection - {{AccessToken}} only appears once in json file ( would expect it to appear twice)
  2. Jenkins pipeline which grabs collection from cloud using postman api - has to be the same json file as export one would assume so same basic problem as 2 ( note using newman to run collection in jenkins)

Given the collection access token is set to a variable not an actual access token I am not sure why this setting disappears.

Hope that helps clarify the issue

Hi @david.thomas,

Welcome to the community! :clap:

Thanks for explaining the steps you have taken and where you are having trouble.

You don’t have to use a collection variable, you can equally do this with an environment variable (though, this original thread was in regards to a collection). You may have better luck using a Postman environment.

Granted though, you are going to see this collection variable change depending on whom else is using it within the team. Environment variables might be easier to control the Initial Value and the Current Value, as the Initial Value would be local to you.

Why is the exported collection different that what you receive via the Postman API? It should be the same, unless someone changed is between the export and when the api call is made to Postman.

Feel free to show some screenshots if you want to expand on this, but just giving it a guess, using Postman environments might better suit your needs.

Hello @odanylewycz
I am doing automation in postman tool. I have to get Azure access token for that I need client secret which is not shared by my client so I am getting it from newman . how can I save that secretkey in environment once the CI/CD pipelineruns? after saving I need to hit access token end point and save the token to environment variable so that actual azure rest api works.

Hi @gr.pittam,

Welcome to the community!

That is strange, as usually your client secret is provided to you by the vendor, so that you can get the token that you need. If you are getting it from newman, there must be some request that you are making in order to get the client secret to then get an access token.

You can save the access token and client secret in your environment by using a post request script (or just script) to using Postman’s node API. There are plenty of examples you can find in the community here that talk about how to save values into environment variables.

Then you can inject the resulting access token into your next request as a variable (using the double curly brace syntax {{var}} ) and the value should resolve.

There is also an automatic way to do this in Postman using the “Generate Bearer Token” button in the Authorization tab. When in the Postman user interface.

You should also be able to find plenty of examples here in the community as well :slight_smile: