0

I'm trying to implement an oauth2 client in Python3 so that I can upload files to github. For a very basic start I'm trying to get a list of authorizations using the API.

This code works:

from subprocess import Popen,PIPE
user = 'MYUSERNAME'
pw   = 'MYPASSWORD'
git_url = "https://api.github.com/authorizations"
res = Popen(['curl','--user',user + ':' + pw,git_url],stdout=PIPE,stderr=PIPE).communicate()[0]
print(res)

This code does not work:

user = 'MYUSERNAME'
pw   = 'MYPASSWORD'
git_url = "https://api.github.com/authorizations"
import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
                          uri=git_url,
                          user=user,
                          passwd=pw)
opener = urllib.request.build_opener(auth_handler)
f = opener.open(git_url)
print(f.read())

In fact, it generates this error:

Traceback (most recent call last):
  File "demo.py", line 18, in <module>
    f = opener.open("https://api.github.com/authorizations")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 375, in open
    response = meth(req, response)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 487, in http_response
    'http', request, response, code, msg, hdrs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 413, in error
    return self._call_chain(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 347, in _call_chain
    result = func(*args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 495, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

I know that there is an existing Oauth2 implementation in python, but it's python2, not python3, and it does a lot more than I need.

I also know that I could just have my Python program call curl, and that's my fallback.

I'd really like to know what I'm doing wrong.

Thanks.

vy32
  • 28,461
  • 37
  • 122
  • 246
  • 1
    I don't know about Oauth, but have you considered building on top of [requests](http://pypi.python.org/pypi/requests), which is Py3 compatible? There are some requests Oauth layers already around (e.g. [here](https://github.com/maraujop/requests-oauth2)), but I don't know if they're tested on Python 3. – Thomas K Apr 09 '12 at 22:11
  • Huh. I'm not familiar with requests. I'm always hesitant to add more dependencies. I'll check it out. Thanks. Why don't you make this an answer, rather than a comment? – vy32 Apr 09 '12 at 22:19
  • Dependencies aren't all that evil - you can always just bundle them. (I made a comment because I had a suggestion, not a solution) – Thomas K Apr 10 '12 at 17:41

1 Answers1

1

I have just posted an answer to another question with a full example using urllib2 from python2. Obviously you are interested in python3, but it shouldn't be to difficult to migrate the code.

Hope that helps,

Community
  • 1
  • 1
pelson
  • 21,252
  • 4
  • 92
  • 99
  • It looks right but I haven't had a chance to check it out yet. I'll try shortly. Thanks. – vy32 Aug 11 '12 at 22:58