0

Here iam using a request module to download the api information from github and code is shown below.

# Creation of Github request

# Import requests
import requests

r = requests.get('https://api.github.com/user', auth=('user','pass'))
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
print(r.text)
print(r.json())

While using this module there is a error

python github.py           
Traceback (most recent call last):
  File "github.py", line 6, in <module>
    r = requests.get('https://api.github.com/user', auth=('user','pass'))
  File "/home/ubuntu/.local/lib/python2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/requests/adapters.py", line 497, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",)

What i have tried:

Replaced the username and password with my authentation details and it gave the following error.

401
application/json; charset=utf-8
utf-8
{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}
{u'documentation_url': u'https://developer.github.com/v3', u'message': u'Bad credentials'}

Tried to work with access token in the settings tab of github but it was no use.

Any help? Please help me solve this problem.

Also requests modules is installed on my system.

5
  • is there any backslash in your password (would be bad luck!) Commented Dec 20, 2016 at 13:40
  • certificate verify failed indicates a self-signed or bad certificate when getting data over HTTPS (not HTTP). Either try changing your url to http:// or fix your certificate. I just noticed you're fetching against github.com, do you have a proxy in between? Commented Dec 20, 2016 at 13:42
  • looks like your client can not validate server certificate Commented Dec 20, 2016 at 13:43
  • It's likely an SNI issue, can you try updating requests? pip install -U requests[security] Commented Dec 20, 2016 at 13:44
  • Why don't you use one of the Github API clients? Commented Dec 20, 2016 at 13:50

1 Answer 1

4
import requests

r = requests.get('https://api.github.com/user', auth=('user','pass'), verify=False)
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
print(r.text)
print(r.json())

This might work, if that's the case.. Then you have trouble verifying the certificate sent from the server (seeing as you're using HTTPS).

There's a whole section about this

Requests verifies SSL certificates for HTTPS requests, just like a web browser. By default, SSL verification is enabled, and Requests will throw a SSLError if it's unable to verify the certificate:

Note that you shouldn't disable TLS/SSL verification for production code, rather investigate why the certificate isn't valid and follow the guide lines from the official documentation.

Manual verification

You can always export the github certificate from say a browser and place the cert in the same directory as your script. Now this should normally not be needed, but as a test.. This should work:

r.get('https://api.github.com', verify='./github.crt')

And again, make sure you've exported the certificate and placed it as github.crt in the same directory as your script.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.