Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insomnia - using OATH2.0 how do I pull the access token into a variable?

Tags:

insomnia

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

like image 445
efultz Avatar asked Feb 28 '19 12:02

efultz


People also ask

How do I add an access token to insomnia?

If you're using Insomnia, start by creating a new GET request (click the plus icon, or use keyboard command+N or control+N on Windows/Linux). In the “Auth” dropdown menu, select “Bearer Token”. Type in your access token in the “TOKEN” field, and type the word “Bearer” in the “PREFIX” field.

How do I get an access token response?

OAuth: The Videocourse For Dummies If the token access request is invalid or unauthorized, then the authorization server returns an error response. The access token is given by the authorization server when it accepts the client ID, client password and authorization code sent by the client application.


2 Answers

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!!

like image 76
NightlyHakr Avatar answered Oct 19 '22 23:10

NightlyHakr


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.
like image 20
geneowak Avatar answered Oct 19 '22 23:10

geneowak