ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.95k stars 1.69k forks source link

ethPM example "Unauthorized for url" #2748

Closed nocibambi closed 1 year ago

nocibambi commented 1 year ago
aiohttp==3.8.3
aiosignal==1.3.1
asttokens==2.1.0
async-timeout==4.0.2
attrs==22.1.0
backcall==0.2.0
base58==2.1.1
bitarray==2.6.0
black==22.10.0
certifi==2022.9.24
charset-normalizer==2.1.1
click==8.1.3
cytoolz==0.12.0
debugpy==1.6.3
decorator==5.1.1
entrypoints==0.4
eth-abi==2.2.0
eth-account==0.5.9
eth-hash==0.5.1
eth-keyfile==0.5.1
eth-keys==0.3.4
eth-rlp==0.2.1
eth-tester==0.6.0b7
eth-typing==2.3.0
eth-utils==1.9.5
executing==1.2.0
frozenlist==1.3.3
hexbytes==0.3.0
idna==3.4
ipfshttpclient==0.8.0a2
ipykernel==6.17.1
ipython==8.6.0
jedi==0.18.2
jsonschema==4.17.1
jupyter_client==7.4.7
jupyter_core==5.0.0
lru-dict==1.1.8
matplotlib-inline==0.1.6
multiaddr==0.0.9
multidict==6.0.2
mypy-extensions==0.4.3
nest-asyncio==1.5.6
netaddr==0.8.0
numpy==1.23.5
packaging==21.3
pandas==1.5.2
parsimonious==0.8.1
parso==0.8.3
pathspec==0.10.2
pexpect==4.8.0
pickleshare==0.7.5
platformdirs==2.5.4
prompt-toolkit==3.0.33
protobuf==3.19.5
psutil==5.9.4
ptyprocess==0.7.0
pure-eval==0.2.2
py-solc-x==1.1.1
pycryptodome==3.15.0
Pygments==2.13.0
pyparsing==3.0.9
pyrsistent==0.19.2
python-dateutil==2.8.2
pytz==2022.6
pyzmq==24.0.1
requests==2.28.1
rlp==2.0.1
semantic-version==2.10.0
six==1.16.0
stack-data==0.6.1
tomli==2.0.1
toolz==0.12.0
tornado==6.2
traitlets==5.5.0
urllib3==1.26.13
varint==1.0.2
wcwidth==0.2.5
web3==5.31.3
websockets==10.4
yarl==1.8.1

What was wrong?

I tried to recreate the ethPM example from the documentation:

import os
os.environ["WEB3_INFURA_PROJECT_ID"] = INFURA-KEY
from web3.auto.infura import w3

w3.enable_unstable_package_management_api()
w3.pm.set_registry("ens.snakecharmers.eth")
ens_package = w3.pm.get_package("ethregistrar", "3.0.0")
I received the following error: ```python-traceback --------------------------------------------------------------------------- HTTPError Traceback (most recent call last) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ipfshttpclient/http_requests.py:123, in ClientSync._do_raise_for_status(self, response) 122 try: --> 123 response.raise_for_status() 124 except requests.exceptions.HTTPError as error: # type: ignore[attr-defined] File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/requests/models.py:1021, in Response.raise_for_status(self) 1020 if http_error_msg: -> 1021 raise HTTPError(http_error_msg, response=self) HTTPError: 401 Client Error: Unauthorized for url: https+ip4://ipfs.infura.io:5001/api/v0/version?stream-channels=true The above exception was the direct cause of the following exception: StatusError Traceback (most recent call last) /home/andras/Projects/Blockchain/scripts/web.py/web3.py-examples-ethPM.py in line 8 26 w3.enable_unstable_package_management_api() 27 w3.pm.set_registry("ens.snakecharmers.eth") ----> 28 ens_package = w3.pm.get_package("ethregistrar", "3.0.0") File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/web3/pm.py:537, in PM.get_package(self, package_name, version) 535 self._validate_set_registry() 536 release_data = self.get_release_data(package_name, version) --> 537 return self.get_package_from_uri(URI(release_data.manifest_uri)) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/web3/pm.py:342, in PM.get_package_from_uri(self, manifest_uri) 332 def get_package_from_uri(self, manifest_uri: URI) -> Package: 333 """ 334 Returns a `Package `__ 335 instance built with the Manifest stored at the URI. (...) 340 * ``uri``: Must be a valid content-addressed URI 341 """ --> 342 return Package.from_uri(manifest_uri, self.web3) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ethpm/package.py:238, in Package.from_uri(cls, uri, w3) 221 @classmethod 222 def from_uri(cls, uri: URI, w3: "Web3") -> "Package": 223 """ 224 Returns a Package object instantiated by a manifest located at a content-addressed URI. 225 A valid ``Web3`` instance is also required. (...) 236 OwnedPackage = Package.from_uri('ipfs://QmbeVyFLSuEUxiXKwSsEjef7icpdTdA4kGG9BcrJXKNKUW', w3) # noqa: E501 237 """ --> 238 contents = to_text(resolve_uri_contents(uri)) 239 validate_raw_manifest_format(contents) 240 manifest = json.loads(contents) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ethpm/uri.py:50, in resolve_uri_contents(uri, fingerprint) 49 def resolve_uri_contents(uri: URI, fingerprint: bool = None) -> bytes: ---> 50 resolvable_backends = get_resolvable_backends_for_uri(uri) 51 if resolvable_backends: 52 for backend in resolvable_backends: File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/eth_utils/functional.py:45, in apply_to_return_value..outer..inner(*args, **kwargs) 43 @functools.wraps(fn) 44 def inner(*args, **kwargs) -> T: # type: ignore ---> 45 return callback(fn(*args, **kwargs)) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ethpm/_utils/backend.py:63, in get_resolvable_backends_for_uri(uri) 57 @to_tuple 58 def get_resolvable_backends_for_uri( 59 uri: URI 60 ) -> Generator[Type[BaseURIBackend], None, None]: 61 # special case the default IPFS backend to the first slot. 62 default_ipfs = get_ipfs_backend_class() ---> 63 if default_ipfs in ALL_URI_BACKENDS and default_ipfs().can_resolve_uri(uri): 64 yield default_ipfs 65 else: File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ethpm/backends/ipfs.py:78, in IPFSOverHTTPBackend.__init__(self) 77 def __init__(self) -> None: ---> 78 self.client = ipfshttpclient.connect(self.base_uri) File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ipfshttpclient/client/__init__.py:124, in connect(addr, base, chunk_size, offline, session, auth, cookies, headers, timeout, username, password) 116 client = Client( 117 addr, base, 118 chunk_size=chunk_size, offline=offline, session=session, 119 auth=auth, cookies=cookies, headers=headers, timeout=timeout, 120 username=username, password=password, 121 ) 123 # Query version number from daemon and validate it --> 124 assert_version(client.apply_workarounds()["Version"]) 126 return client File ~/.virtualenvs/blockchain-scripts/lib/python3.10/site-packages/ipfshttpclient/client/__init__.py:233, in Client.apply_workarounds(self) 224 def apply_workarounds(self): 225 """Query version information of the referenced daemon and enable any 226 workarounds known for the corresponding version 227 ... StatusError: HTTPError: 401 Client Error: Unauthorized for url: https+ip4://ipfs.infura.io:5001/api/v0/version?stream-channels=true ```
kclowes commented 1 year ago

Since you're getting a 401 back, I would guess that your infura key is wrong, but to check, what happens if you do:

import os
os.environ["WEB3_INFURA_PROJECT_ID"] = INFURA-KEY
from web3.auto.infura import w3
w3.isConnected()

w3.isConnected() should return True. If it doesn't, it's likely that there is something wrong with your infura connection.

I also notice that you're using web3.py v5.31 with Python 3.10. You may need to downgrade your Python version in order to get things to work correctly. Python 3.10 isn't supported until web3.py v6.

pacrob commented 1 year ago

@nocibambi - ethPM is being considered for removal with the next major web3.py version. Are you still using it, or have you found another solution?

nocibambi commented 1 year ago

I don't use it. I went through the docs to learn how to use web3.py.