# Authenticating With OAuth2 And Request Actions

It becomes challenging for REST testing tools to authenticate the user when the OAuth2 authorization scheme is applied to an API. OAuth 2 is a multi-step process that requires user input, a valid session on the authorization server, and transferring response data from a response to another request. Advanced REST Client has request actions and cookie sessions that allow automating this process so you will spend less time managing sessions and tokens, and do more with APIs.

In short summary, OAuth2 requires you to have a valid session on the authentication server, then you have to obtain an authorization token using one of predefined or custom authorization methods (token grant type). The token must be used in an HTTP request that requires authenticated user, usually by putting it into the *Authorization* header (*access\_token* query parameter is also used instead of the header). This article focuses on obtaining an implicit grant type token (I am calling it browser flow because it doesn’t require a server component and is designed to work in browsers only) and then using the token in another request without copying/pasting the token all the time.

The first thing we need is to set up a session on the authorization server. Without it, the token request end up being redirected to a login page and therefore the ARC would render this page in response instead of getting token value. Let’s say I want to make a query to Anypoint Exchange to list my API assets. Normally I would go to <https://www.mulesoft.com/exchange/> and log in. Then I would browse the asset in Exchange’s UI. But now I would like to query for my REST APIs assets using the API. To do so we have to set up a session inside the ARC application. Open the application and from the application's menu select Request > Web session >Login to a web service. This brings a prompt to enter the URL of the service.

![ARC menu option to log in to a web service](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWd2WqTPow_OL4IIud%2F1.png?alt=media\&token=5d3ea1b8-f2f8-43b9-a6d5-68f08d1e6376)

Now we enter Exchange’s URL (<https://www.mulesoft.com/exchange/>) and after clicking the “Open” button it will bring a new window, which is Chromium browser, with loaded Exchange UI. Now we navigate to “Log in” page.

![Browser window opened by ARC to setup the session](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWdEQv9eTGxvWdQ8Ej%2F2.png?alt=media\&token=0615d313-ce81-476d-b3c1-419cf9c3becb)

After the login process, the window shows the Anypoint platform page but now we are authenticated.

![Authenticated user in browser window](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWdRiVj8IMzMj8scpK%2Fauthenticated-user-anypoint.png?alt=media\&token=a39a4c68-f4bd-452c-9b49-5b8704b68bcf)

To confirm we can go to cookie manager in ARC (Request > Web session > Cookie manager) and query cookies by domain mulesoft.com.

![Generated session cookies](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWddT48FaKC4KWW_6F%2Fcookie-manager.png?alt=media\&token=5a9878bf-b994-4305-a59c-576162d45272)

To obtain the token we have to call the authorization endpoint. In my case it is `https://anypoint.mulesoft.com/accounts/oauth2/authorize?client_id=AZ8ewU90hIgZMJec&redirect_uri=https%3A%2F%2Fauth.advancedrestclient.com%2Foauth-popup.html&response_type=token`\
The `https://anypoint.mulesoft.com/accounts/oauth2/authorize` is the authorization endpoint. This is where you redirect the user to authenticate with OAuth2. The `client_id` parameter is generated by OAuth2 provider ID when registering an application (here it is not a real client ID, however, this is public information). The `redirect_uri` tells where to redirect the user after login or failure. ARC uses `https://auth.advancedrestclient.com/oauth-popup.html` by default (you can change it in application settings). Finally `response_type` tells the authorization server that this is implicit authentication flow and in response the app expects the token to be set in the redirect URL. Usually, the `scope` parameter is used with OAuth2 request but I will skip it this time.

After providing this URL to ARC’s URL field and executing the request instead of receiving the login page you will see the `redirect_uri` page (the OAuth popup). What is more interesting, the redirect URL contains the `access_token` value in the URL hash part (you may need to toggle “Details” on response view).

![Response with access token](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWeJgkbOSPBdCYlL8I%2Fauthentication-response.png?alt=media\&token=1ec71498-1582-4339-af1e-5f0582bd80c6)

Now we have to extract the access token value and put it into an application variable so it can be later used by other requests. We will use the request actions panel to do that. In the Actions tab create a new action in the After response section. We will tell ARC to get the access\_token value from the response URL and then store it as the AnypointToken variable.

![Response action configuration](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWeXDp9dsiKBhISy4O%2Fauth-response-action-configuration.png?alt=media\&token=4939f486-8ca1-46a4-8fa2-8c6269e7ef92)

The `hash.access_token` value in the “Path to data” field tells to search for the `access_token` parameter in the `hash` part of the response URL. Finally, the configuration tells the application to store the value as the `AnypointToken` variable. Try running the request again. After the response is ready you will see a new variable in the variables preview (right top corner of the app).

![Variables list with new variable](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWeqIBQ3W-qpp-3OoA%2Fvariables-preview.png?alt=media\&token=1fb2a54a-ea46-43a5-a6ff-329161ea0c5f)

At this point, we had authenticated the user in the OAuth service and then we had stored the access token value as an application variable. Now we can use the variable to put the token into the `Authorization` header and make an authenticated call to the API endpoint. Let’s try `https://anypoint.mulesoft.com/exchange/api/v1/profile` which returns profile data for the user. The endpoint requires the bearer token obtained before from the authentication server. In the headers panel put `Bearer ${AnypointToken}`  as the value for `Authorization` header.

![Using variables in headers editor](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWfD0fIo1zcjYj8_Ci%2Fauth-anypoint-variable.png?alt=media\&token=031f9587-d984-4c07-8813-244c5c0657a2)

After calling the endpoint a 200 response is reported back with profile information.

![Authenticated response](https://2300023309-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgJa7u7Cw8HjSR0oUlx%2F-LrWc35CK-QcAwYrSfGm%2F-LrWfM0g8vzE8CpHZl_y%2Fauthenticated-response.png?alt=media\&token=e1423438-7aa4-4e3d-8d44-f4332385bec3)

You can use this example for any other OAuth 2 provider. You may need to change authorization parameters like authorization URI, client id, and so on.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.advancedrestclient.com/guides/authenticating-with-oauth2-and-request-actions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
