20

I am trying to make a call to an authorization endpoint using OAuth2 with grant type Client Credentials - my call is successful - that is not an issue. However, I, now, want to take the access token that is returned and put it in a variable so I may use it in subsequent calls without having to manually cut-and-paste to my other calls.

When the call returns I see the token I desire to copy in the Access Token field at the bottom of the OAuth2 window (the one shown below that says expires in 42 minutes) AND I see it in the Authorization field on the Timeline tab of the results. I just can't figure out how to get access to it so I may dump it into variable.

enter image description here

enter image description here

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
efultz
  • 1,135
  • 2
  • 11
  • 26

5 Answers5

54

The gif on the FAQ goes really fast, and does not provide step by step. Also, I didnt find any answer on YouTube or other websites, so I thought to share step by step for chaining requests on Insomnia.

  1. Create a POST query to obtain your access token. Notice that my access token is returned in the field called "access_token", we will use this in step 3. Your return field may be different.

enter image description here

  1. Create a second GET request for the API that would return the data for you. In my case, I wanted to get all users from a SCIM interface. In the Bearer tab, type in Response => Body Attribute (Insomnia will autofill).

enter image description here

  1. Mouse click on the Request => Body Attribute (the one you just typed in), and select the authentication post in the dropdown "Request" (this is the one you created in step 1), and in the "Filter (JSONPath)" field, type in the $.[attribute name] - where attribute name is the response that returns from authentication call. In my case, it was access_token, see step 1 for yours.

enter image description here

Enjoy!!

NightlyHakr
  • 854
  • 1
  • 7
  • 7
  • The autofill did not work correctly, i had to delete the text before the label manually, than it worked as intended. Any idea how to automagically refresh the token? Thanks a lot for this! – nilsK Jan 28 '22 at 10:05
15
  1. Click No Environment > Manage Environments and you will see a base environment in JSON.
  2. Since this is in JSON, create a { "jwt_token": "Response => Body Attribute" }" pair for your token variable. Please note that "Response => Body Attribute" needs to be configured. When you type response, hit space and this option should be available.
  3. Once done choosing "Response => Body Attribute", it will show with some gibberish content and with red background, no worries... just click it to configure. Make sure you have the same setup. enter image description here

However... you need to change your request to the route where you get the token from the server and another thing is the Filter (JSONPath or XPath) change it depending on your setup.

You should have the token, stored in jwt_token variable and can use the variable on a route that you like.

Example:

enter image description here

5

If you want to save a token that is returned in a response into an environment variable, you can use request chaining in your environment variable. Take a look at this url for more details on that https://support.insomnia.rest/article/43-chaining-requests...

Here is what you could do (what I did)

  1. Create an environment variable
  2. For the value of the variable, use the Response => Body Attribute and under Filter (JSONPath or XPath), choose the attribute of the token in your response body (if it is "token" then put $.token).
  3. After that just put the token environment variable wherever you need it in the following requests.
geneowak
  • 978
  • 11
  • 21
1

I was not able to resolve this question but was able to get around it by defining the fields in the body of the request and bypassing the OAuth2 tab completely.

efultz
  • 1,135
  • 2
  • 11
  • 26
-1

You can add it as a header, by referencing the outputs of the OAuth2 request:

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257