首页 > 解决方案 > Wizaplace API 401 Unauthorized but token worked in the previous request

问题描述

I have a 401 on a request I made on Wizaplace's natively generated API route: GET /api/v1/orders/{{orderId}} (to get anorder using its ID), I'm passing the same token (apiKey) than in previous call to POST /api/v1/basket (to empty the cart) so I don't understand why this is happening. Any help would be welcome !

Here's my code:

async function getOrderById (req, orderId) {
  try {
    console.log(req.header('x-wiza-token'))
    const ret = axios.get(WIZAPLACE_URL + '/orders/' + orderId, {},
      {
        headers: {
          Authorization: req.header('x-wiza-token')
        }
      }) // This is returning 401

    console.log(ret.data)
    return ret.data
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('Error: ', e)
  }
}

app.post('/basket/:id/order', async (req, res) => {
  try {
    console.log('[Order]') // eslint-disable-line
    console.log('URL: ', WIZAPLACE_URL + '/basket/' + req.params.id + '/order') // eslint-disable-line
    console.log('Auth headers: ', req.header('x-wiza-token')) // eslint-disable-line
    console.log('body: ', req.body) // eslint-disable-line

    const ret = await axios.post(WIZAPLACE_URL + '/basket/' + req.params.id + '/order',
      req.body,
      {
        headers: {
          Authorization: req.header('x-wiza-token')
        }
      })

    if (ret.data.orders && !ret.data.html) {
      // This should reset a cart only when and order was successfull
      axios.post(WIZAPLACE_URL + '/basket', {}, {
        headers: {
          Authorization: req.header('x-wiza-token')
        }
      }) // This call is working
    }

    console.log('Order ret: ', ret.data) // eslint-disable-line

    const orders = []

    for (let i = 0; i !== ret.data.orders.length; i++) {
      console.log(ret.data.orders[i])
      orders.push(getOrderById(req, ret.data.orders[i].id))
    }
    console.log(orders)
    res.send(ret.data) // This is working too
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('Error: ', e.response)
    res.status(400).send(e)
  }
})

I really don't know what I'm missing here. Any help on this would be super welcome !

Here's a screenshot of the documentation:

Here's a screenshot of the endpoint's documentation.

标签: axiosnuxt.js

解决方案


推荐阅读