gpt4 book ai didi

python - 在 Flask 中使用 oauth2 库时无法访问 session

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:38 24 4
gpt4 key购买 nike

我已经尝试了几周,试图消除我犯的每一个愚蠢的错误(新手!)。这是我的应用程序工厂功能。我正在关注谷歌应用程序引擎书籍应用程序教程并了解其要点并尝试塑造我自己的东西。我被困在登录功能中。我正在使用包结构。

from woles_search import search#this is blueprint object
from PTE import PTE#this is blueprint object
from Home import home#this is blueprint object
from flask import current_app, Flask, redirect, request, session, url_for,g
import json
import config
import logging
import httplib2
infouser=""#tried to make this variable global and pass it in it templates but didnt worked

# [START include]
from oauth2client.contrib.flask_util import UserOAuth2

oauth2 = UserOAuth2()
# [END include]
#creates a app instance in main directory
def create_app(config):
app=Flask(__name__)
app.config.from_object(config)
#app.config['SECRET-KEY']="secret"
print app.config['SECRET_KEY']
from app_folder.woles_search import search
from app_folder.PTE import PTE
from app_folder.Home import home
with app.app_context():
model = get_model()
model.init_app(app)

oauth2.init_app(
app,
scopes=['email', 'profile'],
authorize_callback=_request_user_info) #configuring oauth

app.register_blueprint(search,url_prefix="/search")
app.register_blueprint(PTE,url_prefix="/PTE")
app.register_blueprint(home,url_prefix="/home")

return app

def get_model():
model_backend = current_app.config['DATA_BACKEND']
print model_backend
if model_backend == 'datastore':
from auth_package import model_datastore
model = model_datastore
else:
raise ValueError(
"No appropriate databackend configured. "
"Please specify datastore, cloudsql, or mongodb")

return model


# [START request_user_info]
def _request_user_info(credentials):
"""
Makes an HTTP request to the Google+ API to retrieve the user's basic
profile information, including full name and photo, and stores it in the
Flask session.
"""
http = httplib2.Http()
credentials.authorize(http)
resp, content = http.request(
'https://www.googleapis.com/plus/v1/people/me')

if resp.status != 200:
current_app.logger.error(
"Error while obtaining user profile: %s" % resp)
return None


infouser = json.loads(content.decode('utf-8'))
#current_app.logger.info(infouser)#this works
session['profile'] = infouser
current_app.logger.info(session['profile'])
#this is a test for session,since it does not show up in templates,it works, get printed out in console

这是主要的.py和app.yaml(复制和粘贴,没什么特别的)main.py

import config
from flask import render_template,current_app,session
from app_folder.app import create_app

app = create_app(config)
app.config.from_object(config)
print app.config['SECRET_KEY']


# Add a logout handler.
@app.route('/logout')
def logout():
# Delete the user's profile and the credentials stored by oauth2.
del session['profile']
oauth2.storage.delete()
return redirect(request.referrer or '/')
# [END logout]


@app.route('/')
def index():
return render_template('base.html')</code>

这是app.yaml

runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.* # This regex directs all routes to main.app
script: main.app
ibraries:
- name: jinja2
version: latest

这是模板

 <html lang="en">
<head>
<title>Bookshelf - Python on Google Cloud Platform</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<div class="navbar-brand">Bookshelf</div>
</div>
<ul class="nav navbar-nav">
<li><a href="/books">Books</a></li>
<li><a href="/books/mine">My Books</a></li>
</ul>
{# [START profile] #}
{{session.profile.displayName}}
<p class="navbar-text navbar-right">
{% if session.profile %}
<a href="/logout">
{% if session.profile.image %}
<img class="img-circle" src="{{session.profile.image.url}}" width="24">
{% endif %}
{{session.profile.displayName}}
</a>
{% else %}
<a href="/oauth2authorize">Login</a>
{% endif %}
</p>
{# [END profile] #}
</div>
</div>
<div class="container">
{% block content %}{% endblock %}
</div>
{{user}}
</body>
</html>
</code>

注意在 if block 之外使用 {{session.profile.displayname}} ,这是为了查看错误是什么,但最终它在 base.html 中也不再显示错误(??),但仍然显示注销 View 中的错误抛出错误“未找到 key 配置文件”。我可以在控制台输出中看到用户信息。我的根目录中有配置文件。我检查了它,它加载了谷歌凭据和 key 。我正在使用谷歌云 sdk shell,因为 gcloud sdk 更新后,我的 dev_appserver.py 在 Windows 上的 cmd 中不再被识别。我撤销了云 sdk shell 上的凭据,只是为了确保它不会干扰(如果有的话!!),并且还尝试了授权。我尝试设置 COOKIE_SECURE_PATH=False 和很多我能想到的事情,但没有结果。请帮助!!!

最佳答案

有太多 Unresolved reference ,我无法测试您的代码。我建议您为小函数编写测试用例,如果可能的话,尝试另一个用于 python appengine 的 oauth 框架,例如 simpleauth(如果它适合您)https://github.com/crhym3/simpleauth我也曾在高级登录框架上遇到过困难,这些框架在 simpleauth 工作之前都无法工作。有什么理由不使用它?我可以帮你。您可以在这里阅读我关于 appengine 中的 Oauth 的问题 How to update User with webapp2 and simpleauth?

关于python - 在 Flask 中使用 oauth2 库时无法访问 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com