在画布页面中使用Facebook Graph API和JS SDK在Safari中的iFrame被破坏了

Aar*_*ter 5 javascript safari facebook-iframe facebook-graph-api facebook-javascript-sdk

所以我正在尝试将Graph API与Facebook JS SDK一起使用,我在Safari中遇到以下错误:

"OAuthException:必须使用活动访问令牌来查询有关当前用户的信息."

我怀疑它与Safari对x-domain cookie设置非常严格的事实有关,所以我在Firefox中尝试了它,cookie选项设置为false FB.init().我确实发现我的FB.api()请求得到了同样的错误.

FB.init({
  appId:  "<%= app_id %>",
  status: true, // check login status
  // We cannot rely on this cookie being set in an iframe. I found this
  // out because the Graph API was not working in Safari.
  // cookie: true, // enable cookies to allow the server to access the session
  xfbml:  true, // parse XFBML
  channelUrl: window.location.protocol + '//' + window.location.host + '/fb/canvas/channel.html'
});
Run Code Online (Sandbox Code Playgroud)

所以我想知道......有没有一种access_tokenFB.api()请求中手动设置查询参数的好方法?

如果FB.init()cookie被正确设置,这就是FB.api()请求参数的样子:

access_token  xxxxxxxxxxxx|1.xxxxxxxxxxxxxxxxxxxxxx__.3600.xxxxxxxxxx-xxx|xxxxxxxxxxxxxxxxxxxxxxxxxxx
callback      FB.ApiServer._callbacks.xxxxxxxxxxxxxxx
pretty        0
sdk           joey
Run Code Online (Sandbox Code Playgroud)

在Safari中(或未cookie FB.init()设置选项时)FB.api()请求参数如下所示:

callback      FB.ApiServer._callbacks.xxxxxxxxxxxxxxx
pretty        0
sdk           joey
Run Code Online (Sandbox Code Playgroud)

现在......显然我的应用程序能够access_token在服务器端生成...我想知道是否有任何方式我可以手动设置FB.api()使用我的服务器端生成access_token.

Aar*_*ter 4

好吧,我已经开始工作了。我发现您不需要该FB.init() cookie: true选项即可使用FB.api()--您可以手动传递 access_token:

FB.api("/me/apprequests/?access_token="+access_token, function (response) {});
Run Code Online (Sandbox Code Playgroud)

哪里access_token是您在页面加载时在服务器端设置的 JS 变量。这是 ERB 的示例:

<script>
  var access_token = "<%= @access_token %>";
</script>
Run Code Online (Sandbox Code Playgroud)

@access_token在初始 POST 请求中传递到画布页面的 oauth access_token。