- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新:当我将我的 nginx 服务器配置为允许 http 并将 Google OAuth 的重定向 URI 设置为使用 http(而不是 https)时,它似乎起作用了。不过,我真的更喜欢使用 HTTPS,所以任何想法都会受到赞赏。
我正在尝试使用 flask-googlelogin 插件授权用户访问我的网络应用程序。运行调试服务器并直接连接到它时一切正常,但是当我在充当反向代理的 nginx 后面运行我的应用程序时(调试服务器或到我在 gunicorn 中运行的应用程序),我在登录回调中收到 400 Bad Request
响应,内容为“浏览器(或代理)发送了该服务器无法理解的请求”。
gunicorn 日志(使用 gunicorn 运行时)没有显示任何有趣的内容。 nginx 日志(在 info
级别)显示返回的 400:
» tail -f /var/log/nginx/*
==> /var/log/nginx/access.log <==
76.164.174.115 - - [21/May/2014:13:07:46 -0500] "GET /login HTTP/1.1" 200 1368 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36"
76.164.174.115 - - [21/May/2014:13:07:49 -0500] "GET /oauth2callback?state=[redacted]&code=[redacted] HTTP/1.1" 400 192 "https://redacted.com/login" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36"
这个 400 似乎来自 flask 。如果我从控制台(不在 gunicorn 中)运行我的应用程序,我会看到以下内容:
» python automation.py
* Running on http://127.0.0.1:8000/
* Restarting with reloader
127.0.0.1 - - [21/May/2014 12:12:17] "GET /login HTTP/1.0" 200 -
127.0.0.1 - - [21/May/2014 12:12:20] "GET /oauth2callback?state=[redacted]&code=[redacted] HTTP/1.0" 400 -
所以 GET 被代理到应用程序,但出于某种原因 flask 不喜欢它。以下是我的应用程序的相关部分:
from flask import Flask, request, render_template, redirect, url_for, flash, session
from flask_login import current_user, login_required, login_user, logout_user, UserMixin, LoginManager
from flask_googlelogin import GoogleLogin
from werkzeug.contrib.fixers import ProxyFix
import json
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.secret_key = "redacted"
app.config.update(
GOOGLE_LOGIN_CLIENT_ID='redacted',
GOOGLE_LOGIN_CLIENT_SECRET='redacted',
GOOGLE_LOGIN_REDIRECT_URI='https://redacted.com/oauth2callback',
)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
login_manager.login_message = u"Please log in to access this page."
login_manager.login_message_category = u"danger"
googlelogin = GoogleLogin(app, login_manager)
class User(UserMixin):
def __init__(self, userinfo):
self.name = userinfo['name']
self.id = userinfo['id']
self.picture = userinfo.get('picture')
users = {
"redacted1" : None,
"redacted2" : None,
}
@googlelogin.user_loader
@login_manager.user_loader
def load_user(userid):
return users.get(userid)
@app.route("/login", methods=['GET', 'POST'])
def login():
return render_template('login.html', login_url=googlelogin.login_url())
@app.route('/oauth2callback')
@googlelogin.oauth2callback
def oauth2callback(token, userinfo, **params):
if userinfo['id'] in users:
user = users[userinfo['id']] = User(userinfo)
login_user(user)
session['token'] = json.dumps(token)
flash("Logged in", "success")
return redirect(params.get('next', url_for('automation')))
else:
flash("That google user is not authorized.", "danger")
return redirect(url_for('login'))
@app.route("/automation", methods=['GET'])
@login_required
def automation():
return render_template('automation.html')
@app.route("/logoff")
@login_required
def logoff():
logout_user()
session.clear()
flash("logged out", "info")
return redirect(url_for('login'))
if __name__ == "__main__":
app.config.update( GOOGLE_LOGIN_REDIRECT_URI='http://localhost:5000/oauth2callback' )
app.run(debug=True)
Flask 中出现 400 Bad Request
错误的常见原因似乎是 View 函数中的未捕获异常,但我已尝试将整个 oauth2callback
减少为空但是 print
和 pass
仍然失败,我没有看到打印输出。
这是我的 nginx 配置的相关部分:
server {
listen 443;
server_name redacted.com www.redacted.com;
root /usr/share/nginx/www;
index index.html index.htm;
ssl on;
ssl_certificate /etc/ssl/certs/ssl-bundle.pem;
ssl_certificate_key /etc/ssl/private/myserver.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
ssl_prefer_server_ciphers on;
location / {
try_files $uri $uri/ @proxy_to_app;
}
location @proxy_to_app {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
提前致谢。
最佳答案
我在使用 uwsgi 时遇到了同样的问题。
我能够通过将以下行添加到我的 /etc/nginx/uwsgi_params
来修复它。
uwsgi_param UWSGI_SCHEME $server_protocol;
我还添加到我的 app.config
app.config['GOOGLE_LOGIN_REDIRECT_SCHEME'] = "https"
祝你好运。
关于python - 将 flask-googlelogin 与 gunicorn 和 nginx 一起使用会导致 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23791335/
您好,我已经完成了我的 React Native Expo 项目的配置。同样在 Andorid 模拟器中工作,但在 iOS 模拟器中它给出错误提示“异常:您的应用缺少对以下 URL 方案的支持:” 我
我正在使用新的 HttpClient 类(WCF REST 初学者工具包的一部分)对 Google 的 map 数据服务进行身份验证。我有我的 ClientLogin 身份验证 token ,但我不确
更新:当我将我的 nginx 服务器配置为允许 http 并将 Google OAuth 的重定向 URI 设置为使用 http(而不是 https)时,它似乎起作用了。不过,我真的更喜欢使用 HTT
我是一名优秀的程序员,十分优秀!