# Automatically retrieve, store, refresh bearer token

**URL:** <https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166>\
**Category:** Help Hub\
**Tags:** authentication\
**Created:** [July 13, 2022, 2:13pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166 "2022-07-13T14:13:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![usrnamesAreHard](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@usrnamesAreHard](https://community.postman.com/u/usrnamesAreHard)\
**Post date:** [July 13, 2022, 2:13pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/1 "2022-07-13T14:13:39Z")

</div>

Hi, I’m new to Postman and I cannot figure out how to store authentication for a collection. Here’s the API: [User Deletion (Delete API)](https://developers.heap.io/reference/user-deletion)

Right now, I’m manually obtaining the auth\_token and copy/pasting it into the auth tab of the deletion and status requests. The tokens expire every 5 mins, so I need to this frequently as I check the deletion request status.

Is there any way to obtain auth automatically and use it as the bearer token for subsequent requests? I’ve been able to do this for Basic Auth in another collection, but “Inherit from Parent” isn’t working in the case of Heap’s API.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [July 13, 2022, 3:07pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/2 "2022-07-13T15:07:32Z")

</div>

Hi @usrnamesAreHard

You could get the request info from the API docs and then build it into a pre-req (so that it is called before every request), using something like this;

```auto
pm.sendRequest({
    url: 'https://postman-echo.com/post',
    method: 'POST',
    header: 'headername1:value1',
    body: {
        mode: 'raw',
        raw: JSON.stringify({ key: "this is json" })
    }
}, function (err, res) {
    console.log(res);
});

```

ref: [Postman pm.sendRequest example · GitHub](https://gist.github.com/gghughunishvili/5672a40eb0830f45839b056f2f517f14)

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [July 14, 2022, 1:13pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/3 "2022-07-14T13:13:46Z")

</div>

Here is what I use for this.

> [@Medium Article: How to Automate OAuth2 Token Renewal in Postman](https://community.postman.com/t/medium-article-how-to-automate-oauth2-token-renewal-in-postman/6630):
>
> Hey everyone, I have seen on a number of forums and stack overflow articles people expressing their frustration with the manual process Postman has for renewing OAuth2 tokens when they expire. I thought I would share a write up I did to help you get past the manual process and have your tokens renewing automatically. Let me know what you think!

---

<div class="post-metadata">

**Author:** ![usrnamesAreHard](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@usrnamesAreHard](https://community.postman.com/u/usrnamesAreHard)\
**Post date:** [July 14, 2022, 2:40pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/4 "2022-07-14T14:40:31Z")

</div>

Thanks, I’ll take a look!

---

<div class="post-metadata">

**Author:** ![usrnamesAreHard](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@usrnamesAreHard](https://community.postman.com/u/usrnamesAreHard)\
**Post date:** [July 14, 2022, 3:13pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/5 "2022-07-14T15:13:07Z")

</div>

Okay, so, I tried this and the auth request is returning Unauthorized, which breaks the script. I can’t figure out what’s wrong with the request:

POST /api/public/v0/auth\_token HTTP/1.1  
Accept: application/json  
Content-Type: application/x-www-form-urlencoded  
Authorization: Basic [my\_app\_id]:[my\_api\_key]  
User-Agent: PostmanRuntime/7.29.0  
Postman-Token: [postman\_token]  
Host: [heapanalytics.com](http://heapanalytics.com)  
Accept-Encoding: gzip, deflate, br  
Connection: keep-alive  
Cookie: heap=[cookie\_value]  
Content-Length: 0

The same `app_id` and `api_key` work when done manually.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [July 14, 2022, 3:24pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/6 "2022-07-14T15:24:00Z")

</div>

Did you base64 encode the app id and key?

---

<div class="post-metadata">

**Author:** ![usrnamesAreHard](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@usrnamesAreHard](https://community.postman.com/u/usrnamesAreHard)\
**Post date:** [July 14, 2022, 3:40pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/7 "2022-07-14T15:40:58Z")

</div>

The documentation doesn’t say it needs to be, but that said, I’ve tried encoding each, but same error.

---

<div class="post-metadata">

**Author:** ![usrnamesAreHard](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@usrnamesAreHard](https://community.postman.com/u/usrnamesAreHard)\
**Post date:** [July 14, 2022, 3:45pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/8 "2022-07-14T15:45:23Z")

</div>

Okay, I made the manual request that is/was working, checked the console log and just copy/pasted that encoded value…which worked. Not sure where my encoding was off, but either way, working now. Thanks again!

---

<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:** [December 12, 2024, 12:54pm UTC](https://community.postman.com/t/automatically-retrieve-store-refresh-bearer-token/37166/9 "2024-12-12T12:54:13Z")

</div>

Closed on December 12, 2024, 12:54pm UTC

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