Fix 403 error

This commit is contained in:
2026-02-23 18:53:36 -05:00
parent 0d4f36b89a
commit 68de685a4b

View File

@@ -9,6 +9,10 @@ import urllib.request
BASE_URL = os.environ.get("UNBOX_API_BASE", "https://api.unbox.tf")
API_KEY_FILE = ".api_key"
DEFAULT_HEADERS = {
"Accept": "application/json",
"User-Agent": "unbox.py/1.0 (+https://github.com/lexzach/unbox.py)",
}
def load_api_key():
@@ -29,7 +33,10 @@ def get_json(path, params=None, headers=None):
url = f"{BASE_URL}{path}"
if query:
url = f"{url}?{query}"
req = urllib.request.Request(url, headers=headers or {})
req_headers = dict(DEFAULT_HEADERS)
if headers:
req_headers.update(headers)
req = urllib.request.Request(url, headers=req_headers)
with urllib.request.urlopen(req) as resp:
return json.load(resp)
@@ -39,11 +46,22 @@ def post_json(path, params=None, headers=None):
url = f"{BASE_URL}{path}"
if query:
url = f"{url}?{query}"
req = urllib.request.Request(url, method="POST", headers=headers or {})
req_headers = dict(DEFAULT_HEADERS)
if headers:
req_headers.update(headers)
req = urllib.request.Request(url, method="POST", headers=req_headers)
with urllib.request.urlopen(req) as resp:
return json.load(resp)
def http_error_details(e):
try:
body = e.read().decode("utf-8", "replace").strip()
except Exception:
body = ""
return body
def main():
use_alt = "--key" in sys.argv[1:]
crate_ref_id = 2 if use_alt else 1
@@ -55,6 +73,9 @@ def main():
crates_resp = get_json("/user/crates", headers=headers)
except urllib.error.HTTPError as e:
print(f"Failed to fetch crates: {e.code} {e.reason}")
body = http_error_details(e)
if body:
print(body)
return 1
except urllib.error.URLError as e:
print(f"Failed to fetch crates: {e.reason}")
@@ -77,9 +98,16 @@ def main():
return 1
try:
unbox_resp = post_json("/unbox", params={"crate_id": crate_id}, headers=headers)
unbox_resp = post_json(
"/unbox",
params={"crate_id": crate_id},
headers=headers,
)
except urllib.error.HTTPError as e:
print(f"Failed to unbox: {e.code} {e.reason}")
body = http_error_details(e)
if body:
print(body)
return 1
except urllib.error.URLError as e:
print(f"Failed to unbox: {e.reason}")