我正在构建一个 Facebook Canvas 应用程序,它使用 Django 在 iframe 中加载。我希望登录过程能够像 Zynga 那样工作。在此方法中,如果您未登录,您将被重定向到 Facebook 登录页面,然后转到应用程序的权限请求页面(没有任何弹出窗口)。
据我所知,Zynga 必须使用 FBML 并且只是转发到如下所示的 URL:
http://www.facebook.com/login.php?api_key=[api_key]&canvas=1&fbconnect=0&next=[return_url]
在 iframe 中加载的 python 应用程序中是否有类似的效果?
有一个方法here这显示了如何使用新的 php sdk 实现正确的重定向,但我正在尝试使用只有方法的新 python SDK:
def get_user_from_cookie(cookies, app_id, app_secret):
"""
Parses the cookie set by the official Facebook JavaScript SDK.
cookies should be a dictionary-like object mapping cookie names to
cookie values.
...
"""
我有一些使用 Javascript SDK 和 get_user_from_cookie 方法的工作代码:
<div id="fb-root">
<script src="http://connect.facebook.net/en_US/all.js"></script>
</div>
<script type="text/javascript">
FB.init({ apiKey: 'apikey', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
// Reload the application in the logged-in state
window.top.location = 'http://apps.facebook.com/myapp/';
});
</script>
<fb:login-button>Install MyApp</fb:login-button>
此方法的问题在于,它需要用户单击按钮进行登录,然后通过弹出的身份验证屏幕进行操作。 (注:直接调用FB.login也会弹窗)
那么...有没有办法使用 javascript SDK 重定向到登录页面而不是将其作为弹出窗口加载?
感谢您的帮助! --埃里克
最佳答案
Is there a way to use the javascript SDK to redirect to the login page rather than loading it as a popup?
没有。 JavaScript SDK 将打开一个新窗口而不是重定向当前窗口。
要向用户显示授权对话框的全屏版本,您需要将他们重定向到 https://graph.facebook.com/oauth/authorize?client_id={{ application_id }}&redirect_uri= {{ redirect_uri }}
。请注意,您不能从服务器端代码执行此操作,因为那样只会重定向 iframe 而 Facebook 不允许这样做。您将需要一个重定向父窗口的中间文档。
<!DOCTYPE html>
<!--
This document redirects the parent window to the authorization
dialog automatically, or falls back to a link if the client does not
support JavaScript.
-->
<html>
<head>
<script type="text/javascript">
window.parent.location = 'https://graph.facebook.com/oauth/authorize?client_id={{ application_id }}&redirect_uri={{ redirect_uri }}';
</script>
</head>
<body>
You must <a target="_top" href="https://graph.facebook.com/oauth/authorize?client_id={{ application_id }}&redirect_uri={{ redirect_uri }}">authorize this application</a> in order to proceed.
</body>
</html>
一旦用户授权您的应用程序,他或她将被重定向到您在 redirect_uri
中指定的 URI,Facebook 将使用各种类型的 GET 参数填充 signed_request
可用于代表用户向 Facebook 发出请求的美味信息(参见 the documentation on signed requests)。
请注意,如果您计划将此签名请求(或其他任何内容)保存到 Canvas 应用程序中的 cookie,则需要在 header 中设置紧凑的 P3P 策略;否则,所有版本的 Internet Explorer 都会忽略您的 cookie。
P3P: CP="IDC CURa ADMa OUR IND PHY ONL COM STA"
我编写了一个库,它可以非常轻松地制作由 Django 提供支持的 Facebook Canvas 应用程序,它会为您处理所有这些事情。它叫做Fandjango ,它在 github 上可用。
关于javascript - 使用新图形 API 在 Facebook Canvas 应用程序中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302908/