5

I am running a REST API (Search API) with Tweepy in Python. I worked the program at home and it's totally fine. But now I am working on this in different networks and I got the error message.

SSLError: ("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",)

My code is like this.

auth = tweepy.AppAuthHandler(consumer_key, consumer_secret) api = tweepy.API(auth,wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

I found this post Python Requests throwing up SSLError and set the following code (verify = false) may be a quick solution. Does anyone know how to do it or other ways in tweepy? Thank you.

Community
  • 1
  • 1
Wei-Ting Liao
  • 115
  • 2
  • 9
  • 1
    Wei, just wondering if you found any permanent solution of this instead of setting SSL verify flag to false. I am facing similar issue - but its random. – Rohit Jun 20 '16 at 08:56
  • Thanks for the questions. Unfortunately... there's no luck now. Right now I just tried to avoid connect that network and use others with different network security setting. Do you have other solutions? – Wei-Ting Liao Jun 21 '16 at 14:35

5 Answers5

2

In streaming.py, adding verify = False in line# 105 did the trick for me as shown below. Though it is not advisable to use this approach as it makes the connection unsafe. Haven't been able to come up with a workaround for this yet.

stream = Stream(auth, listener, verify = False)
kpratihast
  • 842
  • 1
  • 10
  • 23
2

I ran into the same problem and unfortunately the only thing that worked was setting verify=False in auth.py in Tweepy (for me Tweepy is located in /anaconda3/lib/python3.6/site-packages/tweepy on my Mac):

resp = requests.post(self._get_oauth_url('token'),
                             auth=(self.consumer_key,
                                   self.consumer_secret),
                             data={'grant_type': 'client_credentials'},
                             verify=False)

Edit:

Behind a corporate firewall, there is a certificate issue. In chrome go to settings-->advanced-->certificates and download your corporate CA certificate. Then, in Tweepy binder.py, right under session = requests.session() add

session.verify = 'path_to_corporate_certificate.cer'

Nic Scozzaro
  • 6,651
  • 3
  • 42
  • 46
0

First, verify if you can access twitter just using a proxy configuration. If so, you can modify this line on your code to include a proxy URL:

self.api = tweepy.API(self.auth)
Murmel
  • 5,402
  • 47
  • 53
0

Adding verify=False will ignore the validation that has to be made and all the data will be transferred in plain text without any encryption.

pip install certifi

The above installation fixes the bad handshake and ssl error.

onlyvinish
  • 435
  • 1
  • 5
  • 20
0

For anybody that might stumble on this like I did, I had a similar problem because my company was using a proxy, and the SSL check failed while trying to verify the proxy's certificate.

The solution was to export the proxy's root certificate as a .pem file. Then you can add this certificate to certifi's trust store by doing:

import certifi
cafile = certifi.where()
with open(r<path to pem file>, 'rb') as infile:
    customca = infile.read()
with open(cafile, 'ab') as outfile:
    outfile.write(customca)

You'll have to replace <path to pem file> with the path to the exported file. This should allow requests (and tweepy) to successfully validate the certificates.

JCVanHamme
  • 1,050
  • 7
  • 12