php - FB 好友选择器框架位于 https 中,无法与 http 中的托管页面交互

标签 php javascript facebook facebook-graph-api coldfusion

我在网站上实现了邀请好友功能。
最近它停止工作了(由于“稳定”的 Facebook 图形 API 的持续乐趣......)
我正试图解决这个问题,但运气不佳。
每当我与 friend 选择器框架交互时,我都会收到以下错误:

Blocked a frame with origin "https://www.facebook.com" from accessing a frame with origin "http://myDomain.com". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.

我的网络应用程序确实是在 http 而不是 https 上,但这个错误是新的,因为此功能过去一直有效。

这是我用来打开好友选择器框架的代码:

  function sendRequestViaMultiFriendSelector() {
    window.fbAsyncInit();
    FB._inCanvas = true;

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
        // the user is logged in and has authenticated your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire

            FB.ui({method: 'apprequests',
                message: 'my message to my friends goes here'
            }, function(response) {
                console.log('requestCallback');
                console.log('request: '+response.request);
                console.log('to: '+response.to);
                //write response values to DB goes here... 
            });

            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
        } else {
            console.log('sendRequestViaMultiFriendSelector, not connected');

            FB.login(function(response) {
                if (response.authResponse) {
                    console.log('starting again');
                    sendRequestViaMultiFriendSelector();
                } else {
                    console.log('User cancelled login or did not fully authorize.');
                    on_fb_fail();//...
                }
            }, {scope: "email,"+
                        "user_about_me,"+
                        "publish_stream"});

        }
    });
} 

这是我用来初始化 FB api 的代码

 console.log('>> window.environment: '+window.environment);
window.fbAsyncInit = function() {
    console.log('window.fbAsyncInit, window.fb_inited: '+window.fb_inited);
    if(window.fb_inited === false){
    console.log('executing window.fbAsyncInit...');
    //if (navigator.appName.indexOf("Microsoft") != -1) {
    //if(/msie/gi.test(navigator.userAgent) ){
    if(navigator.appName === 'Microsoft Internet Explorer'){
        window.fb_inited = true;    
        FB.init({
            appId                : 'MY_APP_ID', // App ID
            status               : true, // check login status
            cookie               : true, // enable cookies to allow the server to access the session
            xfbml                : true,  // parse XFBML
            oauth                : true,
                    frictionlessRequests : true
        });
    } else {
        window.fb_inited = true;
        FB.init({
            appId                : 'MY_APP_ID', // App ID 
            channelUrl           : 'http://www.myDomain.com/channel.cfm', // Channel File
            status               : true, // check login status
            cookie               : true, // enable cookies to allow the server to access the session
            xfbml                : true,  // parse XFBML
            oauth                : true,
                    frictionlessRequests : true
        });
    }
    console.log('window.fb_inited: '+window.fb_inited);

    }
// Additional initialization code here
};

(function(d, s, id){
    window.fb_inited = false;
    console.log('Loading FB JS-SDK asynchronously...');
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

这是我的 channel .cfm

<cfsilent>
    <cfparam name="url.localeCode" default="en_US" />
    <cfset cacheExpire = 60 * 60 * 24 * 365 />
    <cfheader name="Pragma" value="public" />
    <cfheader name="Cache-Control" value="maxage=#cacheExpire#" />
    <cfheader name="Expires" value="#getHttpTimeString(dateAdd('d', now(), 365))#">
</cfsilent>
<cfoutput>
    <script src="//connect.facebook.net/#url.localeCode#/all.js"></script>
</cfoutput>

这是一个来自channel.php的快速cf端口,我也尝试过......

 <?php
 $cache_expire = 60*60*24*365;
 header("Pragma: public");
 header("Cache-Control: max-age=".$cache_expire);
 header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT');
 ?>
 <script src="//connect.facebook.net/en_US/all.js"></script>

非常感谢任何想法 我听说 Facebook 没有与他们的更改同步更新他们的引用,因此使用他们的 API 是“愉快的”...... 我的脚本基于这些链接: FB.init - https://developers.facebook.com/docs/reference/javascript/ FB 好友请求 - https://developers.facebook.com/docs/reference/dialogs/requests/

最佳答案

我添加了这两行代码来破坏 iFrame,它解决了异常

if (top.location!= self.location){
  top.location = self.location
}

这解决了不同协议(protocol)的问题...
继续解决其他错误...

关于php - FB 好友选择器框架位于 https 中,无法与 http 中的托管页面交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17133716/

相关文章:

php - PHP丢失变量数据

php - 上传多张图片如何存储

php - Laravel - 在子文件夹中返回 View

javascript - 文件输入类型未包含在 AJAX 发送的 jquery FormData 中

javascript - 使用 .height() 方法修改 div 高度后,添加类不会更改高度

Facebook 屏蔽测试账户

java - 将 java 日期转换为 Javascript 日期

javascript - 我的 Blade 文件中的任何 &lt;script&gt; 标签都会导致 [Vue warn] : Error compiling template, 错误——我该如何解决这个问题?

javascript - 通过 Javascript/Phonegap 将 base64 图像上传到 Facebook 图形 API

php - 任何 Facebook PHP SDK v4.06 工作 Canvas 应用示例?无法获得有效 session