javascript Facebook本地开发不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10276077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:17:09  来源:igfitidea点击:

Facebook local development not working

javascriptfacebooklogin

提问by Shaun Budhram

I'm at my wit's end trying to get this to work. It used to, but for some reason seems like it's broken again. I'm only having trouble when developing locally. Is anyone else currently having similar issues with Facebook local development?

我在我的智慧的尽头试图让它发挥作用。它曾经是,但由于某种原因,它似乎又坏了。我只是在本地开发时遇到问题。目前是否还有其他人在 Facebook 本地开发方面遇到类似问题?

These are my steps:

这些是我的步骤:

  1. Create an app
  2. Under the 'Basic Info' section, set 'localhost' as the domain (I have also tried leaving this blank)
  3. Set my website to http://localhost:8080/
  4. Make sure sandbox mode is OFF
  5. My local server is running Javascript at http://localhost:8080/private/that looks basically like below.
  6. I have also tried using a substitute for localhost (like myapp.com), defining it in etc/hosts file mapped to 127.0.0.1, setting the App Domain to 'myapp.com' and using the site 'http://myapp.com:8080/' with no luck.
  1. 创建应用
  2. 在“基本信息”部分下,将“本地主机”设置为域(我也尝试将其留空)
  3. 将我的网站设置为 http://localhost:8080/
  4. 确保沙箱模式关闭
  5. 我的本地服务器正在运行 Javascript http://localhost:8080/private/,基本上如下所示。
  6. 我还尝试使用本地主机的替代品(如 myapp.com),在映射到 127.0.0.1 的 etc/hosts 文件中定义它,将应用程序域设置为“myapp.com”并使用站点“http://myapp.com”。 com:8080/' 没有运气。

FB.getLoginStatus() call is not returning, or only returns occasionally. It always works on the production server.

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));
     }

EDIT: After checking the response for FB.getLoginStatus(..), it is returning this:

编辑:检查 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.

应用程序配置不允许给定的 URL:应用程序配置不允许一个或多个给定的 URL。它必须与 Connect 或 Canvas URL 之一匹配,或者域必须与应用程序的基本域之一相同或子域。

采纳答案by ROFLwTIME

I had the same exact error that you are describing and it was driving me nuts. You have to make sure that your Facebook URL in your Canvas or Page URL matches EXACTLYwhat you are typing in the browser.

我遇到了与您所描述的完全相同的错误,这让我发疯。您必须确保 Canvas 或 Page URL 中的 Facebook URL 与您在浏览器中输入的内容完全匹配。

In my case, I added a "www" to the URL in my address bar (e.g wwww.domain.com/MyApp), but under the Facebook Developer settings I just had domain.com/MyApp as my Page URL without the www prefix. That subtle difference is what was causing the error. Something may be happening similarly in your case.

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

If you are testing via localhost, I recommend creating a new app and making it a test version of your production app. That way, you can have your PROD url and your TEST url under the respective app in order that things don't get messy.

如果您通过 localhost 进行测试,我建议您创建一个新应用程序并将其设为生产应用程序的测试版本。这样,您可以在相应的应用程序下拥有您的 PROD 网址和您的 TEST 网址,以免事情变得混乱。

Also, I recommend debugging using IIS with a custom virtual directory so that you run on port 80. (Facebook seems to act goofy sometimes if you use anything other than 443 or 80) Hope that helps you.

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