You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bitpanda API授权失败:PHP Curl转写及修复求助

Bitpanda API 401未授权问题解决(PHP/JavaScript实现)

问题说明

尝试使用PHP Curl调用Bitpanda API查询加密资产时,请求失败并返回401未授权错误,提示凭证/访问令牌错误。现有可正常运行的PowerShell脚本,需转写为PHP,或提供JavaScript实现方案。

错误信息

{"errors":[{"status":401,"code":"unauthorized","title":"Credentials/Access token wrong"}]}

官方API示例

curl -X GET "https://api.bitpanda.com/v1/trades" \
    -H "X-API-KEY: string"

失效的PHP代码

$service_url = "https://api.bitpanda.com/v1/trades";
$curl_session = curl_init($service_url);

curl_setopt($curl_session, CURLOPT_USERPWD, $apiKey);
curl_setopt($curl_session, CURLOPT_HTTPHEADER, ['content-type: application/json']);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl_session);
curl_close($curl_session);
echo $result;

可用的PowerShell脚本

$key = "APIKEY"
$trades = Invoke-WebRequest -Uri "https://api.bitpanda.com/v1/trades" -Headers @{"X-API-KEY" = $key} -UseBasicParsing
$tdata = $trades.Content | ConvertFrom-Json
$tdata.data.attributes | Out-GridView

修正后的PHP代码

原代码错误在于使用了CURLOPT_USERPWD(Basic认证方式),但Bitpanda API要求通过X-API-KEY请求头传递密钥。修正后的代码如下:

$apiKey = "你的API密钥";
$service_url = "https://api.bitpanda.com/v1/trades";

$curl_session = curl_init($service_url);

// 设置包含X-API-KEY的请求头
curl_setopt($curl_session, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    "X-API-KEY: $apiKey"
]);
curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curl_session);
// 可选:检查Curl请求错误
if(curl_errno($curl_session)) {
    echo 'Curl错误: ' . curl_error($curl_session);
}
curl_close($curl_session);

echo $result;

JavaScript解决方案

浏览器端Fetch实现

const apiKey = "你的API密钥";
const url = "https://api.bitpanda.com/v1/trades";

fetch(url, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'X-API-KEY': apiKey
    }
})
.then(response => {
    if(!response.ok) throw new Error('请求失败');
    return response.json();
})
.then(data => {
    console.log(data.data.attributes);
    // 此处可添加数据处理逻辑
})
.catch(error => console.error('错误:', error));

Node.js环境实现(依赖node-fetch)

先安装依赖:

npm install node-fetch

编写代码:

const fetch = require('node-fetch');

const apiKey = "你的API密钥";
const url = "https://api.bitpanda.com/v1/trades";

fetch(url, {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'X-API-KEY': apiKey
    }
})
.then(response => {
    if(!response.ok) throw new Error('请求失败');
    return response.json();
})
.then(data => {
    console.log(data.data.attributes);
})
.catch(error => console.error('错误:', error));

内容的提问来源于stack exchange,提问作者LLoLoX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 05:35:29