国内使用Google reCaptcha验证码

By Heanny
2020-05-29
4813 read

前言

为啥我出这篇文章呢,因为我有几天用了vaptcha进行人机验证,还算好用,但是发现手机上有广告,本着原则问题,我剔除了人机验证。
又发现在邻居@kidultff发现谷歌国内验证也可以,于是探路V3版本

简介

reCaptcha是Google公司的验证码服务,方便快捷,改变了传统验证码需要输入n位失真字符的特点。
获取https://www.google.com/recaptcha/admin
https://developers.google.com/recaptcha/intro

申请

注册新网站

选择reCAPTCHA 第 3 版或reCAPTCHA 第 2 版,
添加域名(主域名即可)
提交后会给你reCAPTCHA 密钥两个,相当于一个公钥,一个私钥

使用

SCRIPT

<!--国内用这个api-->
<script src='//recaptcha.net/recaptcha/api.js'></script>
<!--国外可以用这个api-->
{#<script src="https://www.google.com/recaptcha/api.js"></script>#}
<script>
    function robotVerified(v) {
        //回调函数中参数为Response值,也可使用grecaptcha.getResponse()进行获取值
        $('#verifiedCode').val(v)
        login()
    }
</script>

V2

<div class="lowin-group password-group">
    <div class="g-recaptcha" data-callback="robotVerified"
    data-sitekey="6LdQg_0UAAAAACmdfbRSxsyDrlRpViwVBeIdB_-6">    </div>
    <input type="hidden" id="verifiedCode">
</div>

V3

<input type="hidden" id="verifiedCode">
<button data-sitekey="6LeGSfwUAAAAANOW_GwwhDjn26HGhde9C5mJBtxz"
data-callback='robotVerified'
data-action='submit' class="g-recaptcha lowin-btn login-btn">
Sign In
</button>

PYTHON后台

code = request.values.get('code')
secret ='6Le*****'
url = 'https://recaptcha.net/recaptcha/api/siteverify?response={}&secret={}&remoteip'.format(code,secret)
req = requests.get(url,headers={})
res = req.json()
if not res['success']:
    return jsonify(code=5, msg='人机验证失败,请重试')

其他

grecaptcha包含的方法,具体使用方法请自行研究

{ready: ƒ, render: ƒ, reset: ƒ, getResponse: ƒ, execute: ƒ}
flask插件之使用flask_caching缓存
flask websocket聊天室

Comments

Heanny #1

V3版本体验地址:https://oauth.heanny.cn/login.html

Leave a Reply