python - 应用引擎上的 Facebook 应用在 Canvas iframe 上显示为空白页面

标签 python facebook google-app-engine iframe

我试图让一个 FB 应用程序显示在它在 facebook 内的应用程序页面上,但 iFrame 只是空白。该应用程序在 localhost 和 appspot 上运行完美,但在 facebook 中加载时没有任何反应。

如果我查看 iframe 的源代码,什么也没有出现,但是如果我刷新此页面,所有代码都显示正常吗?

我试过打开或关闭沙盒,并为 localhost 和 appspot 设置了两个单独的应用程序。两者做同样的事情。

这是我的主要应用程序代码

import cgi
import datetime
import urllib
import wsgiref.handlers
import os
import facebook
import os.path

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app


#local
FACEBOOK_APP_ID = "----------------"
FACEBOOK_APP_SECRET = "---------------"

#live
#FACEBOOK_APP_ID = "--------"
#FACEBOOK_APP_SECRET = "--------------"




class User(db.Model):
    id = db.StringProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True)
    name = db.StringProperty(required=True)
    location = db.StringProperty(required=False)
    profile_url = db.StringProperty(required=True)
    access_token = db.StringProperty(required=True)
    user_has_logged_in = db.BooleanProperty(required=True)

class PageModel:
    def __init__(self, user, friends):
            self.user = user
            self.friends = friends
            #self.length = self.friends['data'].__len__()








class BaseHandler(webapp.RequestHandler):
    """Provides access to the active Facebook user in self.current_user

    The property is lazy-loaded on first access, using the cookie saved
    by the Facebook JavaScript SDK to determine the user ID of the active
    user. See http://developers.facebook.com/docs/authentication/ for
    more information.
    """
    @property
    def current_user(self):
        if not hasattr(self, "_current_user"):
            self._current_user = None
            cookie = facebook.get_user_from_cookie(
                self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
            #if logged in
            if cookie:

                # get user from db
                user = User.get_by_key_name(cookie["uid"])
                # Store a local instance of the user data so we don't need
                # a round-trip to Facebook on every request
                if not user:
                    graph = facebook.GraphAPI(cookie["access_token"])
                    profile = graph.get_object("me")
                    user = User(key_name=str(profile["id"]),
                                id=str(profile["id"]),
                                name=profile["name"],
                                location=profile["location"]["name"],
                                profile_url=profile["link"],
                                access_token=cookie["access_token"],
                                user_has_logged_in = True)
                    user.put()
                #else if we do have a user, but their cookie access token
                #is out of date in the db, update it

                elif user.access_token != cookie["access_token"]:
                    user.access_token = cookie["access_token"]
                    user.put()

                self._current_user = user



        #user = facebook.get_user_from_cookie(self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)            
            friends = "chris"
            pageModel = PageModel(self._current_user, friends)
            return pageModel




        return self._current_user


class Index(BaseHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), "index.html")
        #args = dict(current_user=self.current_user,
        #            facebook_app_id=FACEBOOK_APP_ID)
        args = dict(pageModel=self.current_user,
                    facebook_app_id=FACEBOOK_APP_ID)
        self.response.out.write(template.render(path, args))



application = webapp.WSGIApplication([
  ('/', Index),
  ('/savemyaddress', SaveMyAddress)
], debug=True)


def main():
  run_wsgi_app(application)
  #util.run_wsgi_app(webapp.WSGIApplication([(r"/", HomeHandler)], debug=True))

if __name__ == '__main__':
  main()

我的 JS 在主页上加载

<script>
      window.fbAsyncInit = function() {
        FB.init({appId: '{{ facebook_app_id }}',
                status: true,
                cookie: true,
                 xfbml: true});
        FB.Event.subscribe('{% if pageModel.user %}auth.logout{% else %}auth.login{% endif %}', function(response) {
          window.location.reload();
        });
      };
      (function() {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>

最佳答案

问题是来自 facebook 的第一个请求是一个 post 请求,

//初始 facebook 请求以带有 signed_request 的 POST 形式出现

if u'signed_request' in self.request.POST:
    facebook.load_signed_request(self.request.get('signed_request'))

http://developers.facebook.com/docs/samples/canvas/

所以只要收到一个post请求,应该就OK了。

关于python - 应用引擎上的 Facebook 应用在 Canvas iframe 上显示为空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6594568/

相关文章:

python - CherryPy 将上下文信息添加到日志记录中

css - 如何使 facebook 登录按钮使用 Bootstrap 表单控件?

mysql - 将我们的 Google App Engine Python API 连接到 Digital Ocean 上的外部 MySQL 数据库

java - Google 应用引擎端点在哪里

python - QPushButton 内的 PyQt QHBoxLayout 文本被截断

python - Django 应用程序在本地工作,但在 Heroku 中不工作 : MarkdownX module cannot be found

java - 无法使用 java 通过 selenium webdriver 点击 Facebook "setting"链接

javascript - Facebook 分享按钮的自定义图像

python - 你如何在 Appengine 上使用 xlrd 读取 excel 文件

python - 如何确定 Pandas 列是否包含特定值