Search code examples
pythonsslpraw

SSL Certification verify failed


I've been trying to learn python these past couple of days and I ran into a problem I'm not quite sure how to solve. I'm trying to make a simple reddit bot and learn the praw reddit API. When I run the following bot:

import praw
import time

r = praw.Reddit('testmachine11968986531')
test = r.submission(id="5u7q8x")

comment_user = set()   # to avoid duplicates

for i in xrange(0,10):  # Run the loop 10 times
    #comments = r.comments(submission)
    for comment in test.comments:
        body = comment.body.lower()
        if body.find("think") != -1 or body.find("please") != -1:
            comment_user.add(comment.author)
    #time.sleep(120)   # Sleep for 2 minutes

print "Here are some comments:"
for user in polite_users:
    print user

I get an error:

RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

I've poked around and saw I can insert something like

verify = False

in a get() instance of sorts, but I'm unsure if that would work in this particular example. Everything else works fine I believe -- I can use pip just fine, etc.

Any help would be appreciated. Thanks a ton.

edit: the full error traceback is

 Traceback (most recent call last):
  File "C:\Users\**\Desktop\Bottest\startBot.py", line 16, in <module>
    for comment in test.comments:
  File "C:\Python27\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
    self._fetch()
  File "C:\Python27\lib\site-packages\praw\models\reddit\submission.py", line 133, in _fetch
    'sort': self.comment_sort})
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 320, in get
    data = self.request('GET', path, params=params)
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 404, in request
    params=params)
  File "C:\Python27\lib\site-packages\prawcore\sessions.py", line 133, in request
    self._authorizer.refresh()
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 328, in refresh
    password=self._password)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 138, in _request_token
    response = self._authenticator._post(url, **data)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 29, in _post
    data=sorted(data.items()))
  File "C:\Python27\lib\site-packages\prawcore\requestor.py", line 48, in request
    raise RequestException(exc, args, kwargs)
RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

Solution

  • You problem might reside within your user_agent. Reddit requires a unique user_agent. You user_agent is how you uniquely identify your script. The Reddit API wiki page (https://github.com/reddit/reddit/wiki/API) has the official and updated recommendations on user_agent strings and everything else. Reading it is highly recommended.

    Additionaly to this your user_agent string must never contain the string bot. This means you sould change your line:

    r = praw.Reddit('bot1')
    

    to something like this:

    r = praw.Reddit('Name of your bot [version] by [Your Reddit username or your name]')
    

    [You might want to read this excellent tutorial on the matter https://praw.readthedocs.io/en/v3.6.0/pages/getting_started.html#connecting-to-reddit ]

    Otherwise you can add this after creating the praw object:

    r.config._ssl_url = None
    

    It is not recommended as it means all your information could be intercepted by a man in the middle

    You could also:

    add:

    r.login('bot_username', 'bot_password')
    

    after r = praw.reddit([...])

    change test = r.submission(id="5u7q8x") to test = r.get_submission(submission_id='5u7q8x')

    and add this after:

    test = praw.helpers.flatten_tree(test.comments)
    

    you should now be able to iterate through test and not test.comments.

    If this doesn't work I have no idea what can.