我遇到了关于Tweepy的SSL和证书验证的问题。对于Instagram,我可以使用我公司的证书发送请求并提取所需的用户数据,但它似乎不适用于使用Tweepy python库的Twitter。我已经修改了部分API,以便在向Twitter(cert = company.pem)发出请求时强制添加证书,如下所示:
try:
resp = self.session.request(self.method,
full_url,
data=self.post_data,
timeout=self.api.timeout,
auth=auth,
proxies=self.api.proxy,
**cert="company.pem"**
)
然而,在执行以下代码集时:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, proxy='internet.mycompany.com:443')
print(api.me().name)
我收到以下错误:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 539, in urlopen
self._prepare_proxy(conn)
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 727, in _prepare_proxy
conn.connect()
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connection.py", line 238, in connect
ssl_version=resolved_ssl_version)
File "C:\Python33\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 279, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Python33\lib\ssl.py", line 246, in wrap_socket
_context=self)
File "C:\Python33\lib\ssl.py", line 350, in __init__
raise x
File "C:\Python33\lib\ssl.py", line 346, in __init__
self.do_handshake()
File "C:\Python33\lib\ssl.py", line 553, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:548)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\requests\adapters.py", line 370, in send
timeout=timeout
File "C:\Python33\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 574, in urlopen
raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:548)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python33\lib\tweepy\binder.py", line 188, in execute
proxies=self.api.proxy
File "C:\Python33\lib\site-packages\requests\sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python33\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:548)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/kteh/PycharmProjects/NewTwitterProject/TwitterTest.py", line 18, in <module>
print(api.me().name)
File "C:\Python33\lib\tweepy\api.py", line 337, in me
return self.get_user(screen_name=self.auth.get_username())
File "C:\Python33\lib\tweepy\auth.py", line 136, in get_username
user = api.verify_credentials()
File "C:\Python33\lib\tweepy\api.py", line 630, in verify_credentials
)(**kargs)
File "C:\Python33\lib\tweepy\binder.py", line 241, in _call
return method.execute()
File "C:\Python33\lib\tweepy\binder.py", line 191, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:548)
我已经尝试过针对类似问题进行研究,但我没有发现任何为Tweepy指定证书的提及。设置verify = False不能像旁路那样工作。 python代码在我的家庭网络中正常运行,但不在我的公司网络中运行。我需要使用我公司的证书在公司网络中运行它,就像我使用Instagram Python API一样。
修改:我尝试安装证书,现在又给我一个新错误
tweepy.error.TweepError: Failed to send request: [SSL] PEM lib