0

So I was trying to dive into Python because I want to write an Agent for the Plex Media Server. This Agent will access the MyAnimeList.net API with HTTP Authentication (more about that here) my Username and passwords work but I don't have a clue why I still get a 401 error from the server as response.

Here is some code (I'm using python 2.5 because plex said so) :)

import urllib2

username = "someUser"
password = "somePass"

url = "http://myanimelist.net/api/anime/search.xml?q=bleach"


password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = "http://myanimelist.net/"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(urllib2.HTTPHandler, handler)
request = urllib2.Request(url)
print request.get_full_url()
f = urllib2.urlopen(request).read()

print(f)

and this is what I get as response

> http://myanimelist.net/api/anime/search.xml?q=bleach Traceback (most
> recent call last):   File
> "C:\Users\Daraku\Desktop\MAL.bundle\Contents\Code\__init__.py", line
> 16, in <module>
>     f = urllib2.urlopen(request).read()   File "C:\Python25\lib\urllib2.py", line 121, in urlopen
>     return _opener.open(url, data)   File "C:\Python25\lib\urllib2.py", line 380, in open
>     response = meth(req, response)   File "C:\Python25\lib\urllib2.py", line 491, in http_response
>     'http', request, response, code, msg, hdrs)   File "C:\Python25\lib\urllib2.py", line 418, in error
>     return self._call_chain(*args)   File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
>     result = func(*args)   File "C:\Python25\lib\urllib2.py", line 499, in http_error_default
>     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 401: Unauthorized

Bear in mind that this is my first time programming in Python and im kind of confused with many examples in the web because they did something with the urllib2 so that it doesn't exists in python 3.0, i think, anymore

Any ideas? Or any better ways to do this?

dmg
  • 7,438
  • 2
  • 24
  • 33
Daraku
  • 19
  • 6

0 Answers0