[Python] Invalid Authority - OKX Bitcoin API

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am getting Invalid Authority error when running this code to pull data from OKX. I have confirmed the env vars are correct.

    I think it is something with the signature.

    def generate_signature(timestamp, method, request_path, body, secret_key):
    message = f'{timestamp}{method.upper()}{request_path}{body}'
    signature = hmac.new(secret_key.encode(), message.encode(), hashlib.sha256).digest()
    return base64.b64encode(signature).decode()

    def get_brc20_activities(ticker: str, limit=100):
    url_path = '/api/v5/mktplace/nft/ordinals/trade-history'
    url = 'https://www.okx.com' + url_path
    method = 'POST'
    cursor = ''
    all_transactions = []

    while True:
    payload = {
    'slug': f'brc20_{ticker.lower()}',
    'limit': limit,
    'sort': 'desc',
    'isBrc20': True
    }
    if cursor:
    payload['cursor'] = cursor

    body = json.dumps(payload, separators=(',', ':')) # Ensure compact format
    timestamp = datetime.utcnow().isoformat("T", "seconds") + "Z"
    signature = generate_signature(timestamp, method, url_path, body, API_SECRET)

    headers = {
    'Content-Type': 'application/json',
    'OK-ACCESS-KEY': API_KEY,
    'OK-ACCESS-SIGN': signature,
    'OK-ACCESS-TIMESTAMP': timestamp,
    'OK-ACCESS-PASSPHRASE': API_PASSPHRASE
    }

    response = requests.post(url, headers=headers, data=body)
    if response.status_code != 200:
    print(f"Error fetching data: {response.status_code} - {response.text}")
    break

    data = response.json()
    print(json.dumps(data, indent=2)) # Optional: inspect raw result

    activities = data.get('data', {}).get('data', [])
    all_transactions.extend(activities)

    cursor = data.get('data', {}).get('cursor')
    if not cursor:
    break

    time.sleep(0.25)

    return all_transactions


    ticker = 'FFIE' # Replace with any BRC-20 ticker like 'sats'
    txns = get_brc20_activities(ticker)
    print(f"Retrieved {len(txns)} transactions for {ticker.upper()}")
    for txn in txns[:5]: # print first 5 as preview
    print(txn)

    Continue reading...

Compartilhe esta Página