AndreMiras / EtherollApp

Provably fair dice game running on the Ethereum blockchain
MIT License
57 stars 22 forks source link

BadRequest: <Err: Problem with connection, status code: 403> #156

Closed AndreMiras closed 4 years ago

AndreMiras commented 4 years ago

https://sentry.io/organizations/andre-5t/issues/1248320917/?referrer=github_plugin

BadRequest: <Err: Problem with connection, status code: 403>
(3 additional frame(s) were not displayed)
...
  File "service/main.py", line 136, in pull_account_rolls
  File "service/main.py", line 127, in pyetheroll
  File "home/vagrant/build/com.github.andremiras.etheroll/.buildozer/android/platform/build/build/python-installs/etheroll/pyetheroll/etheroll.py", line 67, in __init__
  File "home/vagrant/build/com.github.andremiras.etheroll/.buildozer/android/platform/build/build/python-installs/etheroll/etherscan/contracts.py", line 12, in get_abi
  File "home/vagrant/build/com.github.andremiras.etheroll/.buildozer/android/platform/build/build/python-installs/etheroll/etherscan/client.py", line 121, in connect

BadRequest: <Err: Problem with connection, status code: 403>
AndreMiras commented 4 years ago

The problem seems to be with Ropsten and requests module user agent. It gets blocked by cloudflare.com

python
Python 3.7.3 (default, Apr  3 2019, 19:16:38) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> response = requests.get('https://api-ropsten.etherscan.io/api?module=contract&address=0xe12c6dEb59f37011d2D9FdeC77A6f1A8f3B8B1e8&apikey=E9K4A1AC8H1V3ZIR1DAIKZ6B961CRXF2DR&action=getabi')
>>> response.status_code
403
>>> response.headers
{'Date': 'Fri, 08 Nov 2019 08:33:45 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=dff366142715f98c20aa41c298c6850371573202025; expires=Sat, 07-Nov-20 08:33:45 GMT; path=/; domain=.etherscan.io; HttpOnly', 'Cache-Control': 'max-age=15', 'Expires': 'Fri, 08 Nov 2019 08:34:00 GMT', 'X-Frame-Options': 'SAMEORIGIN', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Vary': 'Accept-Encoding', 'Server': 'cloudflare', 'CF-RAY': '53263431bdb9d655-MAD', 'Content-Encoding': 'gzip'}
>>> response.request.headers
{'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

I could reproduce it with curl which also returns a 403:

curl --header "User-Agent: python-requests/2.18.4" -s -o /dev/null -w "%{http_code}" "https://api-ropsten.etherscan.io/api?module=contract&address=0xe12c6dEb59f37011d2D9FdeC77A6f1A8f3B8B1e8&apikey=E9K4A1AC8H1V3ZIR1DAIKZ6B961CRXF2DR&action=getabi"

Changing the user agent to anything else would give a 200:

curl --header "User-Agent: Anything Else" "https://api-ropsten.etherscan.io/api?module=contract&address=0xe12c6dEb59f37011d2D9FdeC77A6f1A8f3B8B1e8&apikey=E9K4A1AC8H1V3ZIR1DAIKZ6B961CRXF2DR&action=getabi"

So ideally the py-etherscan-api we're using would do that. But until then we may be able to workaround ourselves