将授权标头添加到http请求时,来自cURL的-u是什么

问题描述 投票:0回答:1

我正在尝试测试Mix Panel的一个API端点。我正在使用Postman执行此操作,在Mix Panel的文档中,他们使用cURL向您展示如何发出请求。当输入URL和请求的POST数据时,它的工作方式是它到达正确的位置,并告诉我需要通过添加授权标头进行身份验证。我很困惑的是,标题的关键是什么?在他们的cURL示例中,它是-u API_SECRET,那么授权标头密钥是'username'吗?

从文档

# this uses a test project API secret, replace ce08d087255d5ceec741819a57174ce5
# with your own API secret
curl https://mixpanel.com/api/2.0/jql \
    -u ce08d087255d5ceec741819a57174ce5: \
    --data-urlencode params='{"from_date":"2016-01-01", "to_date": "2016-01-07"}' \
    --data-urlencode script='function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }'

如果我想创建一个AJAX查询,例如

$.ajax({
        method: 'POST',
        url: 'https://mixpanel.com/api/2.0/jql',
        data: {
            'params': '{"from_date":"2016-01-01", "to_date": "2016-01-07"}',
            'script': '\'function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }\''
        },
        headers: {
            <WHAT GOES HERE>: API_SECRET
        }
        }).then(function success(response){
            console.log('SUCCESS');
            console.log(response)
        }, function error(response){
            console.log('There was an error running JQL');
            console.log(response.error)
});
api curl mixpanel
1个回答
1
投票

在这种情况下,您的API_SECRET是用户名,没有密码。所以使用没有任何“用户名”密钥的curl -u <API_SECRET>:是正确的。

从mixpanel文档中的示例调用https://mixpanel.com/help/reference/data-export-api

授权步骤Data Export API接受基于HTTPS的基本访问身份验证作为授权方法。要发出授权请求,请将项目的API Secret放在Basic访问身份验证标头的“username”字段中。确保您使用HTTPS而不是HTTP - 我们的API拒绝通过HTTP发出的请求,因为这会以纯文本形式通过Internet发送您的API密钥。

示例以下是使用cURL进行的经过适当身份验证的请求的示例:

curl https://mixpanel.com/api/2.0/segmentation/ \ -u YOUR_API_SECRET:\ -d from_date =“2016-02-11”-d to_date =“2016-02-11”-d event =“Viewed Page”

© www.soinside.com 2019 - 2024. All rights reserved.