Search code examples
oauth-2.0postmanpostman-collection-runnerpostman-pre-request-scriptpostman-newman

Need to avoid manual clicking GETNEWACCESSTOEKEN Button in postman


My current project is Oauth 2.0 autho based.At postman,I can generate accesstoken using #Getnewaccesstoken button for my collection.

I dont want to click the generate token button in every time...Is any option there in prerequiste script form for automating this step...


Solution

  • Yes, you can use an access token to assign variable.

    Your current using token

    This is demo, current you use "Get New Access Token" I am using Keycloak v.18, getting token as admin

    enter image description here

    New method using variable

    You can make own POST call for getting access-token and then assign same name variable. Before your API call, you needs this code in Tests section.

    var jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable("access-token", jsonData.access_token);
    

    enter image description here

    POST call, your token URL and key/value for your inputs.

    If you success, will get response in body enter image description here

    How to use it in other API call.

    In the Authorization tab, select Bearer Token Type

    enter image description here

    And Add this code. it is access token.

    {{access-token}}
    

    Then you can call any API like this. Will get the result enter image description here