andelf / tronpy

TRON Python Client Library.
MIT License
204 stars 96 forks source link

403 Client Error #106

Closed krau5 closed 1 year ago

krau5 commented 1 year ago

I have the following line, where I am getting the usdt contract by its address

self._contract: Contract = self._tron_client.get_contract(self._network_data.usdt_token_address)

But when I run the code, I receive

File "/root/app/utils/tron_client.py", line 28, in __init__
  self._contract: Contract = self._tron_client.get_contract(self._network_data.usdt_token_address)
File "/root/venv/lib/python3.10/site-packages/tronpy/tron.py", line 966, in get_contract
  info = self.provider.make_request("wallet/getcontract", {"value": addr, "visible": True})
File "/root/venv/lib/python3.10/site-packages/tronpy/providers/http.py", line 89, in make_request
  resp.raise_for_status()
File "/root/venv/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
  raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.trongrid.io/wallet/getcontract

The API key for TronGrid provider is alright ( it works locally ), but when I try to run the same code on the server - it fails. The only difference is python version ( 3.10 on server & 3.11 locally ), but could it even raise such a problem?

What should I do?

krau5 commented 1 year ago

I've updated the python version. It's not about the python. Why does the code locally and on server executes differently, although it's the same ( I literally cloned the repo and copied the configuration )

MrNaif2018 commented 1 year ago

Hmm, Forbidden usually means trongrid bans ip? Check if maybe it's cloudflare doing it's stuff You can test with ankr RPCs too

krau5 commented 1 year ago

Won't it say on trongrid that the api key has blocked some requests if it banned the ip? By the way, which cloudflare are you talking about?

krau5 commented 1 year ago

I've just created a new API key, no changes, same error + TronGrid does not give any information if the request using exact API key was blocked or sth

MrNaif2018 commented 1 year ago

Trongrid might be behind cloudflare and it might be banning your server. Usually by checking response headers you can know

krau5 commented 1 year ago

But the error is from the tronpy library. It's not the part of my code which fails and I can get the response directly

krau5 commented 1 year ago

Specifically on this line

File "/root/venv/lib/python3.11/site-packages/tronpy/tron.py", line 966, in get_contract
  info = self.provider.make_request("wallet/getcontract", {"value": addr, "visible": True})
MrNaif2018 commented 1 year ago

But the error is from the tronpy library

Not it is not, it is from sending the request to trongrid Try with another rpc like https://www.ankr.com/rpc/tron

krau5 commented 1 year ago

Uhm... but it literally says venv/lib/python3.11/site-packages/tronpy/tron.py in line

info = self.provider.make_request("wallet/getcontract", {"value": addr, "visible": True})

I'll have a look at that rpc, although I do not understand how it's related to the issue

MrNaif2018 commented 1 year ago

Because it sends an internet request, and status code forbidden means server (the rpc, not the library) blocked your request for some reason

krau5 commented 1 year ago

So I can somehow pass the specified rpc inside the library or should I change the approach in the code if so?

krau5 commented 1 year ago

I've made a contact with trongrid, they did not have an explanation either, so I've moved a server from one location to another and it works fine now

krau5 commented 1 year ago

Weird thing, tbh

MrNaif2018 commented 1 year ago

Yeah, as I've told you could switch from trongrid to something better or investiate why cloudflare blocks your server. Glad you got it resolved