Fix 403 errors in get_metadata and execute_sparql (missing User-Agent)#7
Open
oddradiocircle wants to merge 1 commit into
Open
Fix 403 errors in get_metadata and execute_sparql (missing User-Agent)#7oddradiocircle wants to merge 1 commit into
oddradiocircle wants to merge 1 commit into
Conversation
`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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
get_metadataandexecute_sparqlalways fail with HTTP 403 Forbidden, whilesearch_entity,search_property, andget_propertieswork 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 aUser-Agent), butget_metadataandexecute_sparqlcalledclient.get(...)withoutheaders=HEADER, so httpx sent its default UA and Wikimedia returned 403.Reproduced directly against both endpoints:
python-httpx/*)api.phpwbgetentities/sparqlFix
headers=HEADERinexecute_sparqlandget_metadata."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