I am trying to use http://developer.github.com/v3/ to retrieve project issues. This works:
curl -u "Littlemaple:mypassword" https://api.github.com/repos/MyClient/project/issues
It returns all private issues of my client's project. However, I am not able to find out how to implement this in Python. Both ways I have found (e.g. Python urllib2 Basic Auth Problem) doesn't work, they return 404 or 403 errors:
def fetch(url, username, password):
"""Wonderful method found on forums which does not work.""""
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(url)
f = urllib2.urlopen(req)
return f.read()
...and:
def fetch(url, username, password):
"""Wonderful method found on forums which does not work neither.""""
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
return urllib2.urlopen(request).read()
Any ideas? Thanks in advance!
r = requests.get('https://api.github.com', auth=('user', 'pass'))
Python requests
is the way to go here. I've been using requests
extensively at work and at home for various web service interactions. It is a joy to use compared to what came before it. Note: the auth
keyword arg works on any call that requires auth. Thus, you can use it sparingly, i.e. you don't need it for every call against GitHub, only those that require logins. For instance:
r = requests.get('https://api.github.com/gists/starred', auth=('user', 'pass'))
The GitHub login is documented here:
http://pypi.python.org/pypi/requests/0.6.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With