Wenn ich die Blockchain.info-API aufrufe, erhalte ich einen Fehler. Was kann ich tun, um dieses Problem zu lösen?

Ein Skript, das die Blockchain.info-API aufruft, um den UTXO zu finden, der sich auf eine Adresse bezieht

# get unspent outputs from blockchain API

import json
import requests

# example address
address = '1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX'

# The API URL is https://blockchain.info/unspent?active=<address>
# It returns a JSON object with a list "unspent_outputs", containing UTXO, like this:
#{      "unspent_outputs":[
#   {
#     "tx_hash":"ebadfaa92f1fd29e2fe296eda702c48bd11ffd52313e986e99ddad9084062167",
#     "tx_index":51919767,
#     "tx_output_n": 1,
#     "script":"76a9148c7e252f8d64b0b6e313985915110fcfefcf4a2d88ac",
#     "value": 8000000,
#     "value_hex": "7a1200",
#     "confirmations":28691
#   },
# ...
#]}

resp = requests.get('https://blockchain.info/unspent?active=%s' % address)
utxo_set = json.loads(resp.text)["unspent_outputs"]

for utxo in utxo_set:
    print "%s:%d - %ld Satoshis" % (utxo['tx_hash'], utxo['tx_output_n'], utxo['value'])

Wenn Sie dieses Skript ausführen, wird der Fehler angezeigt:

Traceback (most recent call last):
  File "get-utxo.py", line 26, in <module>
    utxo_set = json.loads(resp.text)["unspent_outputs"]
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Antworten (1)

Die API gibt zurück No free outputs to spend, wenn es keine nicht ausgegebene Ausgabe gibt, Sie müssen diesen Fall berücksichtigen. Sie können den Statuscode einfach überprüfen (200: OK, 500: Fehler).