Pre-request script to get access token from Dropbox APIs

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:

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

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

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.

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.