# Pre-request script to get access token from Dropbox APIs

**URL:** <https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893>\
**Category:** Help Hub\
**Tags:** authorization, authentication, pre-request\
**Created:** [October 9, 2022, 12:38am UTC](https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893 "2022-10-09T00:38:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![coco2020](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@coco2020](https://community.postman.com/u/coco2020)\
**Post date:** [October 9, 2022, 12:38am UTC](https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893/1 "2022-10-09T00:38:51Z")

</div>

Hello, I’m new to Postman and API Testing. I’m trying to create a pre-request script to get access token automatically for a Postman collection using Dropbox APIs. However, it didn’t work well.

From what I understand, first I need an authorization code, and after that using that authorization code to get the access token. However, I can’t find a way to automate these 2 processes in one script.

This is my script in Postman:

```auto
pm.sendRequest(
  {
    url: pm.variables.get("Access_token_url"),
    method: "POST",
    header: {
      "Accept": "application/json",
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": pm.variables.get("Auth_url"),
    },
    body: {
      mode: "urlencoded",
      urlencoded: [
        { key: "grant_type", value: "refresh_token", disabled: false },
        {
          key: "client_id",
          value: pm.variables.get("App_key"),
          disabled: false,
        },
        {
          key: "client_secret",
          value: pm.variables.get("App_secret"),
          disabled: false,
        },
        {
          key: "refresh_token",
          value: pm.variables.get("refresh_token"),
          disabled: false,
        },
      ],
    },
  },
  (err, res) => {
    console.log(res.json());
  }
);

```

If I try this way, I will get an error like this:

> error\_description: “Can’t use “Authorization” header and “client\_secret” arg together.”}

Could anyone help me to fix this problem ? Cause later I need to run this collection with command line using Newman to pass all the tests.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [October 14, 2022, 12:06am UTC](https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893/2 "2022-10-14T00:06:32Z")

</div>

May I ask why you are not using a regular Postman request for this?

---

<div class="post-metadata">

**Author:** ![coco2020](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@coco2020](https://community.postman.com/u/coco2020)\
**Post date:** [October 14, 2022, 12:11am UTC](https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893/3 "2022-10-14T00:11:55Z")

</div>

Because it’s my homework, and the mentor asked us to create a pre-script to get a token automatically, and later we will need to run it in nodejs with newman as well as incoporate it into CI Jenkins. I have tried many ways but I dont know how to automate the process to get the authorization code before using it for getting a new token.  
Hope to get some advice here.

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [October 14, 2022, 7:40pm UTC](https://community.postman.com/t/pre-request-script-to-get-access-token-from-dropbox-apis/39893/4 "2022-10-14T19:40:14Z")

</div>

Here is my advice: try getting this to run from a regular Postman request.  
Once that works, and as long as there is no manual interaction needed to get that token, you can go back to your pre-request script.

You can use the Postman console to compare the request working (the regular Postman request) and the request from the pre-request script that is not working.

In this way, you can figure out what is the difference.
