Skip to content

Fix 403 errors in get_metadata and execute_sparql (missing User-Agent)#7

Open
oddradiocircle wants to merge 1 commit into
zzaebok:masterfrom
oddradiocircle:fix/wikidata-user-agent-403
Open

Fix 403 errors in get_metadata and execute_sparql (missing User-Agent)#7
oddradiocircle wants to merge 1 commit into
zzaebok:masterfrom
oddradiocircle:fix/wikidata-user-agent-403

Conversation

@oddradiocircle

Copy link
Copy Markdown

Problem

get_metadata and execute_sparql always fail with HTTP 403 Forbidden, while search_entity, search_property, and get_properties work fine.

Root cause

Wikimedia rejects requests that use a client library's default User-Agent (e.g. python-httpx/...) per the Wikimedia User-Agent policy.

The working tools pass the shared HEADER (which sets a User-Agent), but get_metadata and execute_sparql called client.get(...) without headers=HEADER, so httpx sent its default UA and Wikimedia returned 403.

Reproduced directly against both endpoints:

Request default UA (python-httpx/*) any non-default UA
api.php wbgetentities 403 200
WDQS /sparql 403 200

Fix

  • Pass headers=HEADER in execute_sparql and get_metadata.
  • Replace the placeholder "foobar" User-Agent with a descriptive, policy-compliant value that includes the project URL.

Verification

After the change, all three previously-affected calls succeed:

  • get_metadata("Q739", "es"){'Label': 'Colombia', 'Descriptions': 'país de América del Sur y el Caribe'}
  • execute_sparql(...capital of Q739...)Bogotá
  • get_properties("Q739") → 310 properties

`get_metadata` and `execute_sparql` issued their HTTP requests without
the shared HEADER, so httpx fell back to its default User-Agent
(`python-httpx/...`). Wikimedia rejects default library User-Agents with
HTTP 403, so both tools failed on every call while `search_*` and
`get_properties` (which already pass HEADER) worked.

Changes:
- Pass `headers=HEADER` in `execute_sparql` and `get_metadata`.
- Replace the placeholder `"foobar"` User-Agent with a descriptive,
  policy-compliant value including the project URL, per the Wikimedia
  User-Agent policy (https://meta.wikimedia.org/wiki/User-Agent_policy).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant