【问题标题】:How to use recaptcha without Flask-WTF如何在没有 Flask-WTF 的情况下使用 recaptcha
【发布时间】:2017-06-13 10:44:55
【问题描述】:

我制作文件共享网站已经有一段时间了,我想在人们注册该网站时实施 recaptcha。问题是我不能使用 Flask-WTF,因为我将不得不更改很多代码(我一直在没有它的情况下进行编程)。

我发现这个 Flask recaptcha 不包括使用 Flask-WTF,但我似乎无法让它工作(它不显示 recaptcha 本身):

https://github.com/mardix/flask-recaptcha

我一步步跟着,还是不行。我唯一没有做的就是配置。

编辑: 验证码不起作用。每次我输入正确的注册信息并标记验证码时,它都会说用户名/密码不正确。如果我不标记它,它也会这样做。

这是验证码(其他人之前工作过):

recaptcha = ReCaptcha(app=app)

if recaptcha.verify() is False:
            flash('Captcha is incorrect')
            return redirect(url_for('register'))

    <div id="captcha"">
        {{ recaptcha }} - HTML PART
    </div>

编辑:在得到 Nurzhan 的帮助后,我更改了代码,验证码总是返回 false,无论如何。

【问题讨论】:

  • 如果没有看到您的代码,很难判断它在哪里中断。也许包括路线和模板?此外,该配置可能是扩展工作所必需的。同样,如果没有看到您的实际代码,我们就无法重现。

标签: python flask flask-wtforms


【解决方案1】:

您没有尝试配置,但您需要指明密钥以使您的 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 的官方文档中指出,在客户使用您的 secret_keyg-recaptcha-response 提交您的表单后,您需要单独向 google recaptcha api 发送一个发布请求,当用户在 recaptcha 中打勾时,您会收到这些请求。

请注意,这只是一个示例代码。您需要将自己的 site_key 和 secret_key 添加到 app.pyindex.html 并添加正确检查用户凭据以进行注册,例如重复输入密码等。

【讨论】:

  • 我需要将哪些值放入键中?
  • @T.Simkin 阅读:developers.google.com/recaptcha/intro。它应该解释如何获得一个。
  • 现在可以使用了,非常感谢!知道我只需要弄清楚如何将它与其他设置对齐:)
  • 我还有一个问题。 reCaptcha 用于注册。如果我正确输入了所有内容并且我没有勾选 reCaptcha,则表示用户名/密码而不是 reCaptcha 存在问题。这是代码:如果 recaptcha.verify() 为 False: flash('Captcha is wrong') return redirect(url_for('register'))
  • @T.Simkin 我需要看看这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 2021-06-15
  • 2021-12-28
相关资源
最近更新 更多