api - 将授权 header 添加到 http 请求时,cURL 中的 -u 是什么意思

标签 api curl mixpanel

我正在尝试测试 Mix Panel 的 API 端点之一。我使用 Postman 来执行此操作,在 Mix Panel 的文档中,他们使用 cURL 向您展示如何发出请求。当输入 URL 和请求的 POST 数据时,它会击中正确的位置,并告诉我需要通过添加授权 header 进行身份验证。我感到困惑的是,标题的键应该是什么?在他们的 cURL 示例中,它是 -u API_SECRET,那么授权 header key 会是“用户名”吗?

来自文档

# 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_SECRET是用户名,没有密码。所以使用curl -u <API_SECRET>:没有任何“用户名”键是正确的。

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

Authorization steps The Data Export API accepts Basic access authentication over HTTPS as an authorization method. To make an authorized request, put your project's API Secret in the "username" field of the Basic access authentication header. Make sure you use HTTPS and not HTTP - our API rejects requests made over HTTP, since this sends your API Secret over the internet in plain text.

Examples Here's an example of a properly-authenticated request made with 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"

关于api - 将授权 header 添加到 http 请求时,cURL 中的 -u 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086409/

相关文章:

objective-c - 以编程方式访问 Apple 应用商店

javascript - 直接从浏览器中的输入将 Assets 上传到 contentful (sdk CMA) 时出错

python - 按多个属性过滤的 Mixpanel 数据导出

Java REST API for 循环

javascript - 如何通过 api 调用使用 javascript 自动完成

php - Docker 服务 php-fpm 容器之间的 cURL 导致 "Connection refused"

CURL 错误(无法激活 CURLOPT_FOLLOWLOCATION)

c - S_ISREG 宏未定义

ios - 由于错误而跳过安装 .framework 二进制文件

javascript - 对混合面板的本地主机请求为 "cancelled"