[英]python flask: pass response from a FB API call to back-end
I'm integrating the Facebook SDK for JavaScript into my Python Flask web application. 我正在将JavaScript JavaScript Facebook SDK集成到我的Python Flask Web应用程序中。 I want the ability to access a logged in user's Facebook id server-side. 我希望能够访问已登录用户的Facebook ID服务器端。
My current approach is to build a form containing a hidden input which stores the id and submit it when the user logs in: 我当前的方法是构建一个包含隐藏输入的表单,该表单存储ID,并在用户登录时提交:
<script>
function sendToBackEnd(){
FB.getLoginStatus(function(response){
if (response.status == 'connected'){
$form = $('<form>', {'action': '/process_form', 'method': 'POST'}).appendTO('body');
FB.api('/me', function(response){
$('<input>', {'class': 'hidden', 'name': 'FB_id', 'value': response.id}).appendTo($form);
});
}
});
}
</script>
<div class="fb-login-button" onlogin="sendToBackEnd"></div>
Then store that id in the flask session
object: 然后将该ID存储在flask session
对象中:
@app.route('/process', methods=['GET', 'POST'])
def process():
session['FB_id'] = request.form['FB_id']
print("User's Facebook id now accessible across all routes server-side!")
return redirect(url_for('index'))
Is there a better approach here or is submitting a hidden form the only way to pass this data from client to server-side? 这里是否有更好的方法,还是提交隐藏表格是将数据从客户端传递到服务器端的唯一方法?
Just a thought... I noticed there's a parameter in the FB.init() method called cookie
which is a described as, "Inidcates whether a cookie is created for the session. If enabled, it can be accessed by server-side code." 只是想一想...我注意到FB.init()方法中有一个名为cookie
的参数,描述为:“表明是否为会话创建cookie。如果启用,则可以由服务器端代码访问”。 I'm pretty sure the flask session object can't be modified client-side (rightfully so), but maybe this separate cookie can be assessed server-side in my python code? 我很确定不能在客户端修改flask会话对象(理应如此),但是也许可以在我的python代码中在服务器端评估这个单独的cookie?
You can set the user's fb_id as a session cookie and access it server-side. 您可以将用户的fb_id设置为会话cookie并在服务器端访问它。 This can be done by fb_id = request.cookies.get('COOKIE_NAME')
这可以通过fb_id = request.cookies.get('COOKIE_NAME')
Decided to use a GET or POST request to the Flask app after I login / logout. 登录/注销后决定对烧瓶应用程序使用GET或POST请求。 Creating the form was totally unnecessary and overly complicated. 创建表格完全没有必要,而且过于复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.