使用New Graph API在Facebook Canvas App中进行身份验证

Eri*_*ner 5 javascript python django facebook

我正在构建一个Facebook画布应用程序,它使用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应用程序中实现类似的效果?

有一种方法,在这里展示了如何使用新的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.
...
"""
Run Code Online (Sandbox Code Playgroud)

我有一些使用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>
Run Code Online (Sandbox Code Playgroud)

此方法的问题在于,它要求用户单击按钮进行登录,然后通过弹出认证屏幕进行操作.(注意:如果直接调用FB.login,也会出现弹出窗口)

那么......有没有办法使用javascript SDK重定向到登录页面而不是将其作为弹出窗口加载?

谢谢你的帮助!--Eric

Joh*_*set 13

有没有办法使用javascript SDK重定向到登录页面而不是将其作为弹出窗口加载?

不会.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>
Run Code Online (Sandbox Code Playgroud)

一旦用户授权您的应用程序,他或她将被重定向到您指定的URI redirect_uri,Facebook将signed_request使用各种美味信息(请参阅有关已签名请求的文档)填充GET参数,您可以使用这些信息向Facebook发出请求代表用户.

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

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

我编写了一个库,可以很容易地制作由Django提供支持的Facebook画布应用程序,它可以为您处理所有这些事情.它被称为Fandjango,它可以在github上找到.


aba*_*gat 0

您应该按照有关Canvas 应用程序中身份验证的文档中所述使用 OAuth 。

您已经使用的 Python SDK 包含一个示例,您应该几乎可以开箱即用。