Python - Oauth2的SSL问题 [英] Python - SSL Issue with Oauth2
问题描述
我在Python中使用oAuth2时似乎遇到了一个问题。
这是我的Python脚本(尼斯和简单):
import oauth2.oauth2 as oauth
import urlparse
导入时间
##如果你实际上正在处理请求,你会想要这个
#import simplejson
### GET A REQUEST TOKEN ###
= oauth.Consumer(key =*** KEYHERE ***,secret =*** KEYSECRETHERE ***)
request_token_url ='https://api.instagram.com/ oauth / access_token'
client = oauth.Client(consumer)
resp,content = client.request(request_token_url,GET)
request_token = dict urlparse.parse_qsl(content))
token = oauth.Token(request_token ['oauth_token'],request_token ['oauth_token_secret'])
这些错误来自Python解释器:
回溯(最近一次调用):
文件E:\Projects\oAuth2Test\test.py,第16行,在< module>
resp,content = client.request(request_token_url,GET)
文件E:\Projects\oAuth2Test\oauth2\oauth2.py,行682,请求
connection_type = connection_type)
文件E:\Projects\oAuth2Test\httplib2\httplib2.py,行1445,在请求
(response,content)= self._request
文件E:\Projects\oAuth2Test\httplib2\httplib2.py,第1197行,在_request
($ url,$ url,uri,request_uri,响应,内容)= self._conn_request(conn,request_uri,method,body,headers)
文件E:\Projects\oAuth2Test\httplib2\httplib2.py,第1133行,在_conn_request
conn.connect()
文件E:\Projects\oAuth2Test\httplib2\httplib2.py,第914行,在连接
raise SSLHandshakeError(e)
SSLHandshakeError :[Errno 1] _ssl.c:503:error:14090086:SSL例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
现在,只是这么知道,我有我的cacerts.txt与httplib2在适当的地方,它被发现,但我仍然有这个问题。任何帮助,感谢,谢谢!
解决方案
cacerts.txt
少CA。如果您将其替换为 cacert.pem ,则没有ssl错误。这是一个测试脚本:#!/ usr / bin / env python3
import http.client
import ssl
#### context = ssl.create_default_context(cafile ='cacerts.txt')#ssl.SSLError
#### context = ssl.create_default_context(cafile ='cacert .pem')#works
context = ssl.create_default_context()#工作原理在最近的版本
#NOTE:ssl.CERT_REQUIRED为默认的目的设置.SERVER_AUTH
h = http.client.HTTPSConnection('api.instagram.com',443,context = context)
h.request('POST','/ oauth / access_token')
resp = h.getresponse ()
print(resp.status,resp.reason)#produce expected 400 http error
print(resp.headers)
print(resp.read())
如示例所示,默认CA列表在最近的软件版本上可能就足够了。
I seem to be having an issue with SSL whenever trying to use oAuth2 in Python. I've spent most of the afternoon attempting to debug it but can't seem to figure it out.
Here's my Python Script (Nice and simple):
import oauth2.oauth2 as oauth import urlparse import time ## If you're actually processing requests, you'll want this # import simplejson ### GET A REQUEST TOKEN ### consumer = oauth.Consumer(key="***KEYHERE***", secret="***KEYSECRETHERE***") request_token_url = 'https://api.instagram.com/oauth/access_token' client = oauth.Client(consumer) resp, content = client.request(request_token_url, "GET") request_token = dict(urlparse.parse_qsl(content)) token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
And these error from the Python Interpreter:
Traceback (most recent call last): File "E:\Projects\oAuth2Test\test.py", line 16, in <module> resp, content = client.request(request_token_url, "GET") File "E:\Projects\oAuth2Test\oauth2\oauth2.py", line 682, in request connection_type=connection_type) File "E:\Projects\oAuth2Test\httplib2\httplib2.py", line 1445, in request (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "E:\Projects\oAuth2Test\httplib2\httplib2.py", line 1197, in _request (response, content) = self._conn_request(conn, request_uri, method, body, headers) File "E:\Projects\oAuth2Test\httplib2\httplib2.py", line 1133, in _conn_request conn.connect() File "E:\Projects\oAuth2Test\httplib2\httplib2.py", line 914, in connect raise SSLHandshakeError(e) SSLHandshakeError: [Errno 1] _ssl.c:503: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Now, just so it's known, I have my cacerts.txt that came with httplib2 in the proper place and it is found, yet I still have this problem. Any help is appreciated, thanks!
解决方案
cacerts.txt
contains too few CAs. If you replace it with cacert.pem then there is no ssl error. Here's a test script:#!/usr/bin/env python3 import http.client import ssl ####context = ssl.create_default_context(cafile='cacerts.txt') # ssl.SSLError ####context = ssl.create_default_context(cafile='cacert.pem') # works context = ssl.create_default_context() # works as is on the recent versions #NOTE: ssl.CERT_REQUIRED is set for the default Purpose.SERVER_AUTH h = http.client.HTTPSConnection('api.instagram.com', 443, context=context) h.request('POST', '/oauth/access_token') resp = h.getresponse() print(resp.status, resp.reason) # produce expected 400 http error print(resp.headers) print(resp.read())
As the example demonstrates, the default CA list might be enough on the recent software versions.
这篇关于Python - Oauth2的SSL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!