简体   繁体   English

Javascript REST API - 您必须指定 API 密钥才能发出请求

[英]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 ).尝试从 3rd 方 API ( Klaviyo ) 获取数据时,我收到 403“您必须指定 API 密钥才能发出请求”。

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]}标头:{“授权”:[API_KEY]}

data: {api_key: [API_KEY]}数据:{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).简短回答:将它添加到 params.api_key 下(作为 GET 请求的一部分)。

From the klaviyo documentation :klaviyo 文档
"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." “您通过在每个请求中提供一个私有 API 密钥来对 People API 进行身份验证。(...)身份验证通过每个请求中的 api_key 参数进行。它可以作为 GET 或 POST 参数的一部分发送。”

I think you are using GET request with POST header method.我认为您正在使用带有 POST 标头方法的 GET 请求。 In GET you need to put it in URL在 GET 中,您需要将其放入 URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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