使用新图形API在Facebook画布应用程序中进行身份验证

2024-05-14 16:52:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Django构建一个Facebook画布应用程序,它在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重定向到登录页面,而不是将其作为弹出窗口加载?

谢谢你的帮助! --埃里克


Tags: the方法true应用程序httpfacebookfbcookie
2条回答

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将用各种美味的信息(请参阅the documentation on signed requests)填充GET参数signed_request,您可以用这些信息代表用户向Facebook发出请求。

请注意,如果计划将此签名请求(或任何其他内容)保存到画布应用程序中的cookie,则需要在标题中设置压缩P3P策略;否则,所有版本的Internet Explorer都将忽略您的cookie。

P3P: CP="IDC CURa ADMa OUR IND PHY ONL COM STA"

我已经编写了一个库,它使得使用Django开发Facebook画布应用程序变得非常容易,Django为您处理所有这些事情。它名为Fandjango,在github上可用。

您应该使用OAuth,如关于authentication within Canvas Applications的文档中所述。

您已经在使用的Python SDK包含一个example,您应该可以马上使用它。

相关问题 更多 >

    热门问题