Changeset - a280c27b3c21
[Not reviewed]
default
0 1 0
Mads Kiilerich - 6 years ago 2020-02-04 03:27:17
mads@kiilerich.com
Transplanted from: 9c9344f2242d
py3: fix recaptcha Request parameter type

urllib.parse.urlencode will (after making '%' encoding of unicode characters)
return a str instead of the obviously correct 7-bit ascii bytes.

When we need bytes, we thus have to encode to bytes.
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/recaptcha.py
Show inline comments
 
@@ -25,25 +25,25 @@ def submit(g_recaptcha_response, private
 
    if not (g_recaptcha_response and len(g_recaptcha_response)):
 
        return RecaptchaResponse(is_valid=False, error_code='incorrect-captcha-sol')
 

	
 
    def encode_if_necessary(s):
 
        if isinstance(s, unicode):
 
            return s.encode('utf-8')
 
        return s
 

	
 
    params = urllib.urlencode({
 
        'secret': encode_if_necessary(private_key),
 
        'remoteip': encode_if_necessary(remoteip),
 
        'response': encode_if_necessary(g_recaptcha_response),
 
    })
 
    }).encode('ascii')
 

	
 
    req = urllib2.Request(
 
        url="https://www.google.com/recaptcha/api/siteverify",
 
        data=params,
 
        headers={
 
            "Content-type": "application/x-www-form-urlencoded",
 
            "User-agent": "reCAPTCHA Python"
 
        }
 
    )
 

	
 
    httpresp = urllib2.urlopen(req)
 
    return_values = json.loads(httpresp.read())
0 comments (0 inline, 0 general)