简体   繁体   中英

Javascript REST API - You must specify an API key to make request

I get a 403 "You must specify an API key to make request" when trying to get data from a 3rd party API ( Klaviyo ).

const { id } = req.body

request.get({
    url: `https://a.klaviyo.com/api/v1/person/${id}`,
    headers: {
        api_key: process.env.KLAVIYO_API_KEY
    }
}, (error, response, body) => {
    const profile = JSON.parse(body)
    console.log(profile)
    if (response.statusCode === 200) {
        res.json({ profile, status: 201 })
    } else {
        res.json({ error: 'Did not get customer data', status: 500, response: response, err: error })
    }
})

I've also tried with:

headers: {"Authorization": [API_KEY]}

data: {api_key: [API_KEY]}

Solution:

const { id } = req.body

request.get({
    url: `https://a.klaviyo.com/api/v1/person/${id}`,
    qs: {
        api_key: process.env.KLAVIYO_API_KEY
    }
}, (error, response, body) => {
    const profile = JSON.parse(body)
    console.log(profile)
    if (response.statusCode === 200) {
        res.json({ profile, status: 201 })
    } else {
        res.json({ error: 'Did not get customer data', status: 500, response: response, err: error })
    }
})

Short answer: add it under params.api_key (as part of the GET request).

From the klaviyo documentation :
"You authenticate to the People API by providing one of your private API keys as part of each request. (...) Authentication happens via the api_key parameter in each request. It can be sent as part of the GET or POST parameters."

I think you are using GET request with POST header method. In GET you need to put it in URL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM