Forum Discussion

thatsthat's avatar
thatsthat
Contributor
2 years ago
Solved

Oauth2 Access Token Form Field Fetch Value from Previous Test Step JSON

Using the latest version of ReadyAPI

Test step 1 is a login request which returns an Oauth2 authorization in my JSON response

 

For Test Step 2 I attempt to fetch the token from the Step 1 response and populate it in the Access Token field of the Authorization form.

When I run my request it sends Authorization: Bearer ${#TestCase#access}  instead of picking up the token from Step 1.

 

Any suggestions to solve this?

 

 

  • Hey thatsthat 

     

    Just to be clear - so I'm understanding.  You have a request that contained within the response includes the access token required for Authorisation - (like Access Token=eyJ0eXAiOiJKV1)

     

    you might not even need to use the OAuth authentication - it would help if you could provide all the headers in the subsequent request - but it kinda sounds like  you don't actually need all the OAuth config to setup your request.

     

    Have you had to setup all the other parameters in the OAuthv2 config?

     

    Anyway - have you tried leaving the authentication/authorisation blank on the second call and just passing the token value to and Authorization header on the second call?  Yes I know this sounds silly - but this has worked for me - the OAuth v2 config is required if you have to go through the approx 6 calls to hit the Resource Owner, the Authorisation Server and the Resource Server to subsequently gain the access token required.  However - once you've got the access token you actually need - the just needs the token in the Auth header to hit the Resource Server with your final call.  Sometimes I've had to go use the OAuthv2 authorisation config to go through those 6 steps, sometimes I've had to just create 6 separate REST calls to do it (it's a long story and a bit of a nightmare to setup manually), but other times the architectural setup has been such that I just need to hit an endpoint to get the access token and then pass this onto the Authorization header of the subsequent call.

     

    What I'd suggest trying:

     

    Assuming 

     

    1.  On your FIRST REST call (which returns the Access Token), extract the 'authorization' attribute value and save it to a property (TestCase, or TestSuite) - for this example, I'm gonna assume we're saving at TestSuite level and calling the TestSuite level property 'access_token'.

    2.  On your SECOND REST call, leave the Auth config blank (don't edit it at all or if you have already revert it to blank or Inherit from parent or something), manually add in an 'Authorization' header with the value of 'Bearer ${#TestSuite#access_token}'  (don't add the quote marks I've added in.

     

    Hopefully the above is clear - I would add screenshots - but I'm working on the wrong laptop at moment and this one doesn't have ReadyAPI! installed.

     

     

    Note:  The reason why I'm saying save the access token as a property rather than just pass directly to the SECOND call is so it can be reused by other tests in your testsuite.

     

    Try this and report back

     

    Nice one!

     

    Rich

4 Replies

  • richie's avatar
    richie
    Community Hero

    Hey thatsthat 

     

    Just to be clear - so I'm understanding.  You have a request that contained within the response includes the access token required for Authorisation - (like Access Token=eyJ0eXAiOiJKV1)

     

    you might not even need to use the OAuth authentication - it would help if you could provide all the headers in the subsequent request - but it kinda sounds like  you don't actually need all the OAuth config to setup your request.

     

    Have you had to setup all the other parameters in the OAuthv2 config?

     

    Anyway - have you tried leaving the authentication/authorisation blank on the second call and just passing the token value to and Authorization header on the second call?  Yes I know this sounds silly - but this has worked for me - the OAuth v2 config is required if you have to go through the approx 6 calls to hit the Resource Owner, the Authorisation Server and the Resource Server to subsequently gain the access token required.  However - once you've got the access token you actually need - the just needs the token in the Auth header to hit the Resource Server with your final call.  Sometimes I've had to go use the OAuthv2 authorisation config to go through those 6 steps, sometimes I've had to just create 6 separate REST calls to do it (it's a long story and a bit of a nightmare to setup manually), but other times the architectural setup has been such that I just need to hit an endpoint to get the access token and then pass this onto the Authorization header of the subsequent call.

     

    What I'd suggest trying:

     

    Assuming 

     

    1.  On your FIRST REST call (which returns the Access Token), extract the 'authorization' attribute value and save it to a property (TestCase, or TestSuite) - for this example, I'm gonna assume we're saving at TestSuite level and calling the TestSuite level property 'access_token'.

    2.  On your SECOND REST call, leave the Auth config blank (don't edit it at all or if you have already revert it to blank or Inherit from parent or something), manually add in an 'Authorization' header with the value of 'Bearer ${#TestSuite#access_token}'  (don't add the quote marks I've added in.

     

    Hopefully the above is clear - I would add screenshots - but I'm working on the wrong laptop at moment and this one doesn't have ReadyAPI! installed.

     

     

    Note:  The reason why I'm saying save the access token as a property rather than just pass directly to the SECOND call is so it can be reused by other tests in your testsuite.

     

    Try this and report back

     

    Nice one!

     

    Rich

    • thatsthat's avatar
      thatsthat
      Contributor

      richie Yes! You are a champion mate .. everything you said worked as is cheers. You are correct this is not your typical Oauth2 process is it .. I did the whole properties thingy and good to go.

       

      -thatsthat

      • richie's avatar
        richie
        Community Hero

        Hey thatsthat 

         

        glad it worked out fella - bit surprised - normally I'm wrong about EVERYTHING!

         

        Nice one,

         

        Rich