2017-06-13 77 views
2

我一直在建立一個文件共享網站很長一段時間,我想在人們註冊到網站時實施recaptcha。問題是我不能使用Flask-WTF,因爲我將不得不改變我的許多代碼(我沒有編寫代碼)。如何在沒有Flask-WTF的情況下使用recaptcha

我發現這瓶驗證碼不包括使用瓶,跆拳道,但我似乎無法使它工作(它不顯示的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> 

編輯:從努爾然得到幫助後,我改變了代碼和驗證碼總是返回false,不管是什麼。

+0

爲登記註冊沒有看到你的代碼,這是一種很難說出它打破。也許包括路線和模板?此外,配置可能是擴展工作所必需的。再次看不到您的實際代碼,我們無法重現。 – Maarten

回答

0

您沒有嘗試配置,但您需要指明密鑰才能使您的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_keyg-recaptcha-response

請注意,這只是一個示例代碼。您需要將自己的site_key和SECRET_KEY添加到app.pyindex.html並添加用戶證書的適當檢查像雙輸入密碼等

+0

我需要將哪些值放入密鑰中? –

+0

@ T.Simkin請閱讀:https://developers.google.com/recaptcha/intro。它應該解釋如何得到一個。 – Nurjan

+0

現在有用,非常感謝!知道我只需要弄清楚如何將其與其他設置對齊:) –

相關問題