javascript - Facebook 本地开发不起作用

标签 javascript facebook authentication

我已无计可施,无法让它发挥作用。它曾经是,但由于某种原因似乎又坏了。我只是在本地开发时遇到麻烦。目前还有其他人在 Facebook 本地开发方面遇到类似问题吗?

这些是我的步骤:

  1. 创建应用
  2. 在“基本信息”部分下,将“localhost”设置为域(我也试过将此留空)
  3. 将我的网站设置为http://localhost:8080/
  4. 确保沙盒模式关闭
  5. 我的本地服务器在 http://localhost:8080/private/ 运行 Javascript,基本上如下所示。
  6. 我也尝试过使用本地主机的替代品(如 myapp.com),在映射到 127.0.0.1 的 etc/hosts 文件中定义它,将应用域设置为“myapp.com”并使用站点“http://myapp.com:8080/' 运气不好。

FB.getLoginStatus() 调用未返回,或仅偶尔返回。它始终在生产服务器上运行。

    function myAppInit() {

        console.log("Facebook loaded; initializing sdk.");

        //Initialize FB SDK
        FB.init({ 
            appId:_FbAppId, //This is valid and matches app ID
            cookie:true, 
            status:true, 
            xfbml:true,
            oauth:true
        });

        //Query for login status.
        FB.getLoginStatus(function(response) {
            updateLoginStatus(response);  //This is hit randomly
        });

        //Set our standard callback for auth change status
        FB.Event.subscribe('auth.authResponseChange', function(response) {
           updateLoginStatus(response);   //This is hit randomly
        });    
    }


    $(document).ready(function(){

        window.fbAsyncInit = myAppInit;
        (function(d){
             var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
             js = d.createElement('script'); js.id = id; js.async = true;
             js.src = "//connect.facebook.net/en_US/all.js";
             d.getElementsByTagName('head')[0].appendChild(js);
        }(document));
     }

编辑:检查 FB.getLoginStatus(..) 的响应后,它返回:

Given URL is not allowed by the Application configuration: One or more of the given URLs is not allowed by the Application configuration. It must match one of the Connect or Canvas URLs or domain must be the same as or a subdomain of one of the Application's base domains.

最佳答案

我遇到了与您描述的完全相同的错误,这让我抓狂。您必须确保您在 Canvas 中的 Facebook 网址或页面网址与您在浏览器中输入的内容完全匹配。

在我的例子中,我在地址栏的 URL 中添加了一个“www”(例如 wwww.domain.com/MyApp),但是在 Facebook 开发者设置下,我只是将 domain.com/MyApp 作为我的页面 URL 而没有www 前缀。这种细微的差异是导致错误的原因。您的情况可能会发生类似的情况。

如果您通过本地主机进行测试,我建议您创建一个新应用并将其作为生产应用的测试版本。这样,您就可以在相应的应用程序下拥有您的 PROD url 和 TEST url,以免事情变得困惑。

此外,我建议使用带有自定义虚拟目录的 IIS 进行调试,以便您在端口 80 上运行。(如果您使用 443 或 80 以外的任何其他端口,Facebook 有时似乎表现得很愚蠢)希望对您有所帮助。

关于javascript - Facebook 本地开发不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10276077/

相关文章:

javascript - 背景图片幻灯片过渡问题

javascript - 在页面加载时运行脚本的 chrome 扩展

php - 是否可以让 Facebook session 保持事件状态?

azure - 使用 MSAL.js 和 Azure Active Directory 保护 SPA 中的 REST API 调用 - 如何将身份验证 token 传递给承载策略

node.js - @azure/msal Node : Is there a way to log out/invalidate tokens?

javascript - Quirks 中的 DispHTMLDivElement 现在是 IE9 中的 HTMLDivElement,方法 TransformNode 未定义

javascript - 使用选项 ID 选择

java - 使用 facebook API 的 Stream.publish 方法时获取权限错误

python - facebook-graph sdk 无法处理 unicode 字符

c# - Windows Azure REST API SharedKeyLite 身份验证(存储模拟器)