Catchat 实现 Twitter 的 Oauth 登录时报错 Python Certificate verify failed


#1

Github 和 Google 的 Oauth 都成功了,只有 Twitter,点击用 Twitter 登录就会出现这个问题,(localhost:5000/login/twitter)请问如何修复?
这个错误与申请 twitter 开发者帐户时,创建 app 时填写的信息有关吗?twitter 不允许 app 的 website url 是 127.0.0.1 之类的,所以随便填写了一个自己部署在 heroku 的一个网站的 url。

之前在网上做了一些搜索,有几种说法,一种是说要安装 certificate。我在 command line 里运行了这个命令:

pip install certifi

得到结果:

Requirement already satisfied: certifi in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (2018.4.16)

没解决问题。另一种说是可以在urllib.urlopen()中传递一个 context=ssl._create_default_htttps_context 的参数,但是不太清楚如何把这段代码插入我们的项目代码里?

错误回溯信息如下:

Traceback (most recent call last):
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/me/Documents/mystuff/myproject/fwd/chatroom/app/blueprints/oauth.py", line 96, in oauth_login
    return providers[provider_name].authorize(callback=callback)
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask_oauthlib/client.py", line 523, in authorize
    token = self.generate_request_token(callback)[0]
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask_oauthlib/client.py", line 593, in generate_request_token
    uri, headers, method=self.request_token_method,
  File "/Users/me/.local/share/virtualenvs/chatroom-Pn6Wg-IT/lib/python2.7/site-packages/flask_oauthlib/client.py", line 404, in http_request
    resp = http.urlopen(req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1241, in https_open
    context=self._context)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1198, in do_open
    raise URLError(err)

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726

#2

@greyli
请问大佬能帮忙看下这个问题吗?十分感谢了。


#3

不好意思,最近比较忙,下周有时间来看下。