Forum Discussion

Behdad's avatar
Behdad
Contributor
4 years ago

How to use access token in authorization from another API

Hi

I am using JWT token in my Rest APIs and as it has expiration time , i need to refresh/get new token during my test case 

So here is an example :

1. first test step --> I create new Token using auth/api-key endpoint

2. second test step --> use this token in another Api endpoint to create new client (/client/create) as access token in authorization 

In this case i try to use data transfer between this 2 step , and receive new token but can manage to use it as access token in target !

 

in between , i am not good in scripting so i prefer to do it via available tools & pages in ReadyAPI 3.5.0

 

Thanks in advance

  • richie's avatar
    richie
    Community Hero
    Hey Behdad,


    Can you clarify? I didnt understand where you state "In this case i try to use data transfer between this 2 step , and receive new token but can manage to use it as access token in target !"

    The way i have setup token property transfer before is i have a disabled setup suite that contains the api that generates the auth token. I have a property transfer step in this setup suite that moves the auth token value to a Project level property.

    I have my functional test case (containing the REST step that USES the token) within a enabled test suite. The first test step in my functional testsuite is a RunTestCase step that executes the generate auth token testcase/step that is in the disabled setup suite (which when executed updates the Project level property with the new auth token value).
    My next step is my functional REST step that sources the auth token value from the Project level property.

    The above approach means if you run the project, the disabled test suite doesnt execute unless its called by a functional test suite. Another advantage to this approach is that you dont have to worry about the token expiring, cos the first step in each functional test suite is the RunTestcase test step which re-executes the teststep that generates the auth token.

    Does that help? As i said above, i didnt really understand what you meant, so ive kindof guessed as to what you asked.

    Ta

    Rich
    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you Richie!

       

      Behdad does this help? Please let us know🙂

    • Behdad's avatar
      Behdad
      Contributor

      Hi  

      Many thanks for your suggestion, that is somehow  what I do for now , but it is not automated way to handle that case and some how we need to run test case two time and transfer the token manually.

      let me gave more clear detail and hope you find a way to handle it 🙂

      1-I have Rest api as first test step that create JWT token (with Josn request) and receive token in response as string

      2- So i can add this response as new JWT token and be able to use it for property transfer (for example)

      3- In my next rest service/test step, i need to use this token in Authentication tab as Access token and then run it as second test step but cant! (or i dont know how ?)

      That is my issue , i am looking for a way to do it via same test case and run both of these steps in same test case.

       

       

      • richie's avatar
        richie
        Community Hero

        Hey Behdad 

         

        Ok - so what you're saying is - you have a single testcase with 2 REST requests in there.

         

        The 1st REST request generates a response that includes the auth token

        The 2nd REST request needs the auth token, but you're not sure how to do the property transfer between the two steps (or transfer to a Properties step or transfer to the TestCase level property), or whatever- is that correct?

         

        Apologies - but there's no point me trying to answer a question  you don't want answered - hence I just like to be absolutely clear what you mean!

         

        ta

         

        rich