# How to access inherited authorization in request\`s pre-req. script?

**URL:** <https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169>\
**Category:** Help Hub\
**Tags:** authentication, collection, pre-request-script\
**Created:** [July 3, 2023, 12:19pm UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169 "2023-07-03T12:19:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnlschls](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@dnlschls](https://community.postman.com/u/dnlschls)\
**Post date:** [July 3, 2023, 12:19pm UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/1 "2023-07-03T12:19:18Z")

</div>

When using oAuth 2.0 Authorization on a request…  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/d/9de7b077511360767ce532a94d6aaedd049c596b.png)  
…you can easily access the accessToken from the request via

```auto
pm.request.auth.oauth2.get('accessToken')

```

However, when you use the “inherit auth from parent” as authorization…  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/6/e63e6875ae39d2244e0f4695600120f9288501d6.png)  
…you cannot.

I’ve already tried…

```auto
pm.collectionVariables.auth.oauth2.get('accessToken');

```

… since the oAuth 2.0 authorization is set up in the collection, but also this won´t work.

Similar threads i found to this topic - but no solution to the problem is given:  
[Reading Authorization header in pre-request script - Help - Postman](https://community.postman.com/t/reading-authorization-header-in-pre-request-script/33300)  
[Determining auth type in a pre-request script - Help - Postman](https://community.postman.com/t/determining-auth-type-in-a-pre-request-script/1289)

---

<div class="post-metadata">

**Author:** ![zintgraff9](https://avatars.discourse-cdn.com/v4/letter/z/8e8cbc/32.png) [@zintgraff9](https://community.postman.com/u/zintgraff9)\
**Post date:** [July 5, 2023, 2:55am UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/2 "2023-07-05T02:55:42Z")

</div>

1. Make the authorization call
2. In the Test tab, extract the token and store it as a collection variable.
3. At the collection level, in the authorization tab, set the authorization as the collection variable set in the authorization call.

Then all other requests in the collection can inherit from the parent - collection

---

<div class="post-metadata">

**Author:** ![dnlschls](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@dnlschls](https://community.postman.com/u/dnlschls)\
**Post date:** [July 5, 2023, 12:18pm UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/3 "2023-07-05T12:18:29Z")

</div>

The huge benefit of using the oAuth 2.0 Authorization Feature of Postman is to switch between multiple sessions via the “Manage Token” dropdown without having to re-login. Also you can make use of the “auto-refreh token” feature that way. By switching between sessions the “tests” tab is not executed.  
When setting up a manual authorization request you loose the benefit described above.

So thanks for the hint, but this does not help me that concern.

---

<div class="post-metadata">

**Author:** ![kurtopel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kurtopel/32/29504_2.png) [@kurtopel](https://community.postman.com/u/kurtopel)\
**Post date:** [July 25, 2024, 6:53pm UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/4 "2024-07-25T18:53:53Z")

</div>

Hey @dnlschls,  
I was able to get the accessToken from the Collection and put it into a collection variable. Then one can access the collection variable using `pm.collectionVariables.get("authToken")`

Here is what needs to happen:

1. In your Collection _Variables_ tab, create a variable named `accessToken` without any value.
2. In your Collection _Scripts_ tab, create a _Post-res_ script with this:

```auto
var authToken = pm.request.auth.oauth2.get("accessToken");
pm.collectionVariables.set("authToken", authToken);

```

This gets the oauth2 accessToken in the Collection and stores it in the authToken collection variable for later access!

Now one can access the authToken anywhere in the collection via `pm.collectionVariables.get("authToken")`.

---

<div class="post-metadata">

**Author:** ![dnlschls](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@dnlschls](https://community.postman.com/u/dnlschls)\
**Post date:** [July 26, 2024, 7:37am UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/5 "2024-07-26T07:37:31Z")

</div>

Thank you very much @kurtopel  
this is exactly what i was looking for.

One addition to your answer:

- there is no need to create a variable in first place, postman will create it automatically when it´s not existent
- in your provided example, a variable “authToken” would be created, the variable “accessToken” is not used (probably typo in your answer).

The solution works for the token, that is being used in that request (accessToken), other tokens like the refreshToken cannot be extracted this way, i found.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [July 29, 2024, 7:38am UTC](https://community.postman.com/t/how-to-access-inherited-authorization-in-request-s-pre-req-script/49169/6 "2024-07-29T07:38:16Z")

</div>

Closed on July 29, 2024, 7:38am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
