Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4883

Beginners • urllib API 403 error on Raspberry Pi4B, not on Anaconda Windows or Red Hat Linux

$
0
0
Hello,

I'm trying to get a short python program running on my Pi 4B running Bullseye OS. The program uses urllib.request to hit a specific API, which should return JSON.

Code:

import urllib.requesturl = 'https://xmplaylist.com/api/station/holidaytraditions'# url = 'https://api.github.com/events' -- works!hdr = {"User-Agent":"Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",       "Accept":"application/json"}req = urllib.request.Request(url, headers=hdr)with urllib.request.urlopen(req) as response:    print(response.status)
When I run this, the program errors out on the urllib.request.urlopen(req) line, giving an HTTP ERROR 403: Forbidden error.

The trouble is, i can run the same 5 lines of code on my Windows laptop in an Anaconda python environment running the same version of python (3.9.2) and get a successful response code, 200 (more than that, have decoded and parsed the json, I get what's expected).

I can also do the same on a remote hosting site running Red Hat Linux, and python version 3.6.1.

The commented-out URL in my code above gives me a successful connection on my Pi4B.

Lastly, I can copy the URL above into my Chromium browser on the Pi4B, and it returns the json as expected. The header in my code comes from the "User-Agent" in the Chromium dev tools window after a successful connection. I have tried my python code both with and without the header.

Any ideas on why this errors in python on my Pi4B?

Statistics: Posted by HudsonMoore — Sat Dec 28, 2024 6:39 pm



Viewing all articles
Browse latest Browse all 4883

Trending Articles