You need to enable JavaScript to run this app. Python调用Blocknative websocket API报timeStamp非ISO格式错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Blocknative websocket API报timeStamp非ISO格式错误如何解决

问题修复方案

你遇到的报错不是时间戳格式本身的问题,是代码里的几个低级错误导致的:

  • 字段名错误:Blocknative要求的时间戳字段是驼峰命名的timeStamp,你的代码里写的是全小写的timestamp,服务端读取不到正确的时间戳字段才会报格式错误
  • 字典方法误用:Python的字典update()方法没有返回值,执行后直接修改原字典,你代码里的initial_msg最终会被赋值为None,发送的内容完全不符合要求
  • 额外拼写错误:你定义的字段blockhain少写了一个字母c,正确应为blockchain
  • 发送格式错误:websocket发送需要传入JSON字符串,不能直接传Python字典

你当前用的datetime.datetime.utcnow().isoformat() + 'Z'本身是符合要求的,只要调整逻辑即可,修复后的完整代码如下:

import datetime
import json
from websocket import create_connection
# 替换为你自己的apikey
apikey = "你的实际apikey"

def get_required_parameters():
    required_parameters = {
        # 修正为驼峰命名的timeStamp,指定毫秒精度避免不同Python版本返回位数不一致
        'timeStamp': datetime.datetime.utcnow().isoformat(timespec='milliseconds') + 'Z',
        'dappId': apikey,
        'version': '1',
        # 修正拼写错误的blockchain
        'blockchain': {'system': 'ethereum', 'network': 'bsc-main'}
    }
    return required_parameters

ws = create_connection('wss://api.blocknative.com/v0')
# 先初始化字典,再调用update合并参数
initial_msg = {
    'categoryCode': 'initialize',
    'eventCode': 'checkDappId'
}
initial_msg.update(get_required_parameters())
# 转成JSON字符串后再发送
ws.send(json.dumps(initial_msg))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 20:24:00