您沒有嘗試配置,但您需要指明密鑰才能使您的recaptcha正常工作。這兩個選項的配置是不可選:
RECAPTCHA_SITE_KEY : Public key
RECAPTCHA_SECRET_KEY: Private key
設置他們正確的價值觀,並看看是否可行。
編輯:
它的工作現在。這是app.py:
import requests
import json
from flask import Flask, render_template, request
from flask_recaptcha import ReCaptcha
app = Flask(__name__)
app.config.update({'RECAPTCHA_ENABLED': True,
'RECAPTCHA_SITE_KEY':
'site_key',
'RECAPTCHA_SECRET_KEY':
'secret_key'})
recaptcha = ReCaptcha(app=app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['GET', 'POST'])
def submit():
print('SUBMIT CALLED')
username = ''
password = ''
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
print(request.form)
if username == 'username' and password == 'password':
print('CREDENTIALS ARE OK')
r = requests.post('https://www.google.com/recaptcha/api/siteverify',
data = {'secret' :
'secret_key',
'response' :
request.form['g-recaptcha-response']})
google_response = json.loads(r.text)
print('JSON: ', google_response)
if google_response['success']:
print('SUCCESS')
return render_template('profile.html')
else:
# FAILED
print('FAILED')
return render_template('index.html')
# if recaptcha.verify():
# # SUCCESS
app.run(debug=True)
這是index.html頁面:
<!DOCTYPE html>
<html>
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Flask Recaptcha</h1>
<p>Flask Recaptcha Test</p>
<form method="post" action="/submit">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="password">
{{ recaptcha }}
<input type="submit" value="Submit">
<div class="g-recaptcha" data-sitekey="site_key"></div>
</form>
</body>
</html>
這是profile.html頁面,如果你通過驗證:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Profile page</h1>
<p>Registration is ok</p>
</body>
</html>
我不能不要讓recaptcha.verify()
工作。在Google Recaptcha的官方文檔中,聲明您需要在客戶提交您的表單後,分別向google recaptcha api發送一個發佈請求,其中包括您在用戶打勾時使用的secret_key
和g-recaptcha-response
。
請注意,這只是一個示例代碼。您需要將自己的site_key和SECRET_KEY添加到app.py
和index.html
並添加用戶證書的適當檢查像雙輸入密碼等
爲登記註冊沒有看到你的代碼,這是一種很難說出它打破。也許包括路線和模板?此外,配置可能是擴展工作所必需的。再次看不到您的實際代碼,我們無法重現。 – Maarten