在 Rails 应用程序中检测 Facebook 画布
我使用 before_filter 来检测 Facebook 在用户被引用到画布应用时生成的 signed_request
查询字符串。
然后,我设置 session[:canvas] = true
并根据用户是在画布中还是在本机浏览器应用程序上需要不同的应用程序逻辑进行测试。问题是,如果用户出于任何原因离开画布并导航到基于浏览器的应用程序,则 session[:canvas]
变量仍设置为 true。
有没有更好的方法来检测画布和本机浏览器应用程序之间的差异?
I'm using a before_filter to detect the signed_request
query string Facebook generates when a user is referred to a canvas app.
Then, I set session[:canvas] = true
and test for that when I need different app logic based on whether the user is in the canvas or on the native browser app. The problem is that if the user, for any reason, leaves the canvas and navigates to the browser-based app, the session[:canvas]
variable is still set to true.
Is there a better way to detect the difference between the canvas and the native browser app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人喜欢为 Facebook 应用程序使用“别名”网址,例如在应用程序设置中使用
fb.mysite.com
而不是www.mysite.com
并进行设置使两个域指向同一个地方。或者可以使用目录完成类似的操作,例如www.mysite.com/fb/
指向与www.mysite.com/
相同的位置,但提供了一种简单的方法用于确定是直接访问还是来自应用程序的代码。使用会话也可以工作,但是如果您当前处于“应用程序模式”(canvas==true),则必须添加额外的 javascript 检查。 JavaScript 只是检查页面是否位于 iframe 内,如果不是,则会重定向到类似
www.mysite.com/thispage?app=0
的内容。您的页面应检查app=0
参数并清除会话(如果存在)(或设置 canvas=false)。这样,如果用户从 Facebook 开始,然后直接访问您的网站,事情会自动正确调整。I personally like to use an "alias" url for the Facebook app, e.g. use
fb.mysite.com
instead ofwww.mysite.com
in the app settings and set things up so that the two domains point to the same place. Or something similar can be done with directories, e.g.www.mysite.com/fb/
pointing to the same place aswww.mysite.com/
but giving an easy way for the code to determine if it's a direct access or from an app.Using a session can work too, but you have to add an additional javascript check in the case you are currently in "app mode" (canvas==true). The javascript just checks to see if the page is inside an iframe, and if it is not then it redirects to something like
www.mysite.com/thispage?app=0
. Your pages should check for theapp=0
parameter and clear the session if present (or set canvas=false). This way, if a user starts out inside Facebook but then visits your site directly, things automatically get adjusted correctly.不要在会话中存储此信息,而是检查signed_request参数是否存在,如果没有参数,则可能意味着用户不再位于Facebook应用程序内。
Instead of storing this information at the session, check for the existence of the signed_request parameter, if there is no parameter, it possibly means the user is not inside the facebook app anymore.
我可能完全错了,但 Facebook 不是通过 POST 而不是 GET 请求来访问您的画布内容吗?这难道不是区分请求来自何处的最简单方法吗?
I might be completely wrong, but doesn't Facebook access your canvas content by a POST instead of a GET request? Wouldn't that be the easiest way to distinguish where the request came from?