-1

enter image description hereI am trying to get an acesss token from an api endpoint in postman using the basic authentication flow.

app.post('/epic', async (req:Request, res) => {
  const code = req.query.code as string
  const url = "https://api.epicgames.dev/epic/oauth/v1/token"
  const values = new URLSearchParams({
    code,
    client_id,
    client_secret,
    scope: "basic_profile",
    grant_type: "authorization_code",
  })

  console.log(code, values)

  try {
    const res = await axios.post(url, values, {
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
    })
    console.log(res.data)
    return res.data
  } catch (error: any) {
    console.error(error.message)
    throw new Error(error.message)
  }
})

It keeps returning a 400 bad request. am i doing something wrong?

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
Mhd
  • 817
  • 1
  • 8
  • 21
  • Share more about the error – Jack Sep 13 '21 at 10:31
  • UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch() – Mhd Sep 13 '21 at 10:32
  • Look onto your console, what error is thrown from the axios.post call. So, share the output of this: `console.error(error.message)` from your catch block – Kamesh Sep 13 '21 at 10:36
  • it says "Request failed with status code 400" and the code parameter was undefined. but i am passing it from postman. thanks – Mhd Sep 13 '21 at 10:38
  • does it print code correctly on your console? I see `console.log(code, values)`, is this logged on console? How are you calling API from postman, can you share – Kamesh Sep 13 '21 at 10:40
  • it does prints the values correctly but the code returns undefined. i have added a screenshot – Mhd Sep 13 '21 at 10:44
  • You are probably missing the Authorization header which epic says on their site see `Requesting an access token` https://dev.epicgames.com/docs/services/en-US/EpicAccountServices/GettingStarted/index.html#4.authenticatingepicgamesusersonawebsite – Jack Sep 13 '21 at 10:47
  • Hi jack. the authorization header has been included but its the same result – Mhd Sep 13 '21 at 10:51

1 Answers1

1

req.query gives you the query parameters in the URL (e.g. https://www.somewebsite.com/api?code=supersecretcode), whilst in postman you're providing it as the body of the request. You can go about this two ways:

  1. Use query parameters in the URL instead of in the body in your postman request - this is as simple as moving everything that's in your request body to the URL (http://localhost:4000/epic?code=supersecretcode&grant_type=authorization_code&scope=basic_profile)

  2. Parse the request body in your server. I'm using the helpful body-parser package in this example:

const bodyParser = require("body-parser")

app.use(bodyParser.urlencoded({ extended: false })

app.post('/epic', async (req: Request, res) => {
  const { code } = req.body
  const url = "https://api.epicgames.dev/epic/oauth/v1/token"
  const values = new URLSearchParams({
    code,
    client_id,
    client_secret,
    scope: "basic_profile",
    grant_type: "authorization_code",
  })
  // ...
})
iamkneel
  • 1,303
  • 8
  • 12
  • great! i have rewritten the query and i am getting the 'code'. however the request doesnt gets resolved. it still returns a 400 status code which is weird – Mhd Sep 13 '21 at 11:23
  • Maybe you should try responding to the request, with `res.status(200)`, which will give a status of 200. you can also add some json to your response like `res.status(200).json({ data: res.data })` – iamkneel Sep 14 '21 at 12:57
  • Also not to be greedy or anything, but I'd really appreciate it if you click that green tick on my question to signify that I've answered your initial question :) – iamkneel Sep 14 '21 at 12:58