【问题标题】:Read cookies not set by Flask读取 Flask 未设置的 cookie
【发布时间】:2018-04-12 04:22:38
【问题描述】:

我正在尝试将 Facebook 集成到我的 Flask 应用程序中。 Facebook SDK for JavaScript 无缝且非常易于使用。但是我在传递数据服务器端时遇到了麻烦(我真正需要的是用户的 facebook ID)。这是我的准系统烧瓶应用程序:

from flask import Flask, render_template, session, request, redirect, url_for

app = Flask(__name__)
app.config['SECRET_KEY'] = '123'

@app.route('/')
def index():
    print('cookies', request.cookies)
    return render_template('get_fb_cookies.html')

@app.route('/set_key')
def set_key():
    session['key'] = 'value'
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(debug=True)

这是我的准系统模板,它允许用户登录/注销(并利用 facebook api 的各种其他部分)客户端:

<!doctype html>
<html lang="en">
<body>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '<app-id>',
        cookies    : true,
        version    : 'v2.10' // use graph api version 2.10
      });
      FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
      });
    };

    // Load the SDK asynchronously
    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";  // minified version version
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    // This is called with the results from from FB.getLoginStatus().
    function statusChangeCallback(response) {
      if (response.status === 'connected') {
        FB.api('/me', function(response){
          console.log('Successful login for: ' + response.name);
          console.log('ID: ' + response.id)
        });
      } else {
        console.log('Not connected.')
      }
    };
  </script>

  <div id="fb-root"></div>
  <h1>Get Facebook Cookies</h1>
  <div class="fb-login-button" data-max-rows="1" data-size="medium" data-button-type="login_with" data-show-faces="false" data-auto-logout-link="true"></div>

</body>
</html>

阅读此SO question,看来我需要访问服务器端的cookies,但request.cookies 只是给了我flask.session 设置的cookie。也许我需要更改 Facebook cookie 集的 。现在大约有十个设置为.facebook.com。我想我需要将它们设置为localhost(或者我的服务器正在运行的任何地方)。

这是可行的还是我需要考虑一种不同的方法来访问用户的 facebook ID 服务器端?

【问题讨论】:

    标签: javascript python facebook facebook-graph-api cookies


    【解决方案1】:

    我建议不要通过尝试更改域来污染 FB 的 cookie,而是建议在 statusChangeCallback 中对 Flask 应用程序使用 GET 或 POST 请求

    【讨论】:

    • 好主意。谢谢!
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多