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

使用Coinbase Sandbox API遇授权及JSON解析错误求解决方案

问题描述

我尝试用Coinbase沙箱模式测试策略,已按官方文档创建Sandbox API密钥,但运行Python脚本时出现requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)错误。用curl命令curl -X GET https://api.coinbase.com/v2/accounts -u "API_KEY:API_SECRET"调用返回Unauthorized,确认密钥和网站一致,请问哪里操作有误?

使用的Python脚本如下:

from coinbase.wallet.client import Client
import os
from dotenv import load_dotenv

# Load environment variables from a .env file
load_dotenv()

# Your API credentials from Coinbase
API_KEY = os.getenv("API_KEY")
API_SECRET = os.getenv("API_SECRET")

# Initialize the Coinbase client
client = Client(API_KEY, API_SECRET)

def get_cash_balance():
    # Get accounts
    accounts = client.get_accounts()

    # Find and print the cash balances
    for account in accounts.data:
        if account['type'] == 'fiat':
            print(f"Currency: {account['currency']}")
            print(f"Balance: {account['balance']['amount']}{account['balance']['currency']}")

if __name__ == "__main__":
    get_cash_balance()
解决方案

核心问题:混淆了Coinbase Consumer API和Exchange Sandbox API

你创建的是Coinbase Exchange沙箱API密钥,但代码里用的是coinbase.wallet.client(对应Coinbase Consumer API,即普通Coinbase账户的API),两个API体系不兼容,因此会出现授权失败和JSON解析错误(非200响应返回的不是合法JSON)。

修复步骤:

  1. 切换到Exchange沙箱的Python客户端
    需要使用cbpro库(Coinbase Pro/Exchange的官方Python客户端),而非coinbase库。先安装:

    pip install cbpro
    
  2. 修改Python脚本适配沙箱环境
    初始化客户端时指定沙箱API地址,并使用Exchange沙箱的密钥(注意沙箱密钥创建时会生成专属的API_PASSPHRASE,需一并配置):

    import cbpro
    import os
    from dotenv import load_dotenv
    
    load_dotenv()
    
    API_KEY = os.getenv("API_KEY")
    API_SECRET = os.getenv("API_SECRET")
    API_PASSPHRASE = os.getenv("API_PASSPHRASE")
    
    # 初始化沙箱客户端
    client = cbpro.AuthenticatedClient(
        API_KEY,
        API_SECRET,
        API_PASSPHRASE,
        api_url="https://api-public.sandbox.exchange.coinbase.com"
    )
    
    def get_cash_balance():
        # Exchange沙箱返回格式与Consumer API不同
        accounts = client.get_accounts()
        for account in accounts:
            # 筛选法币账户并输出余额
            if account['currency'] in ['USD', 'EUR', 'GBP']:
                print(f"Currency: {account['currency']}")
                print(f"Balance: {account['balance']} {account['currency']}")
    
    if __name__ == "__main__":
        get_cash_balance()
    
  3. 修正curl命令适配沙箱
    Exchange沙箱的API地址和认证逻辑与Consumer API不同,正确的curl命令如下(需替换为你的沙箱密钥信息):

    curl -H "CB-ACCESS-KEY: 你的API_KEY" \
         -H "CB-ACCESS-SIGN: $(echo -n "$(date +%s)GET/accounts" | openssl dgst -sha256 -hmac "你的API_SECRET" -binary | base64)" \
         -H "CB-ACCESS-TIMESTAMP: $(date +%s)" \
         -H "CB-ACCESS-PASSPHRASE: 你的API_PASSPHRASE" \
         https://api-public.sandbox.exchange.coinbase.com/accounts
    

    注意:需确保系统已安装openssl工具。

额外注意事项

  • 确认沙箱密钥是在Coinbase Exchange沙箱控制台创建的,而非普通Coinbase账户的API密钥。
  • 沙箱环境的账户独立于真实账户,需先在沙箱界面充值测试资金才能查询到有效余额。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 20:23:14