且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

FB.ui拼图:不一致的错误代码191 ...(似乎FB.init经常不工作)

更新时间:2023-12-06 15:44:22

所以,Nitzan值得赞赏,因为他的洞察力的评论,但这里是解决方案。



我得到的错误信息是事实上, FB.init()未加载,或至少没有按照正确的顺序加载到页面的其余部分。从Facebook开发者文档中复制代码,它会异步加载...这可能是一个很大的痛苦的屁股...



所以,而不是什么我已经转过来加载旧的方式:

  FB.init({
appId:'# #########,// App ID
channelUrl:'//www.xxxxxxxxxx.com/channel.php',// Channel File
status :true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true //解析XFBML
});

这与其他脚本的一些重新排序相结合似乎已经解决了我的问题。 >

I'm using FB.ui() like so:

<script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '##########', // App ID
          channelUrl : '//www.xxxxxxxxxx.com/channel.php', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });

        // Additional initialization code here
      };

      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>

Then, here's the link to send the message:

<a href='#' onClick="
        FB.ui({
          method: 'send',
          name: 'Bla bla bla',
          link: 'http://www.xxxxxxxxxxx.com',
          to: ###########,
          //redirect_uri: 'http://www.xxxxxxxxxxx.com/fb/'
          });
        ">Send a message</a>

PROBLEM: This works like a charm for me and every computer/browser I've tested on. But my client gets the following error message very frequently:

API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application

This has me totally stumped! Is anything wrong with my code? And if so, why can't I EVER reproduce this bug while my client consistently can on multiple computers/browsers?

PS: If you want to try yourself, the page is live here. You'll have to authorize the app, but I promise nothing creepy will happen.

EDIT: The error mentions the redirect_uri, which you'll notice is commented out in my code. The reason is because when I include that parameter, the dialogue doesn't close when I hit "close".

EDIT2: I was able to reproduce this bug on a friend's computer, and CBroe also confirmed it. So, (setting aside the mystery of why I still can't produce it myself), the thing that has me most stumped is why does this only happen half of the time?? If my code is incorrect it should never work, right??

Here's the url from the error message:

https://www.facebook.com/dialog/send?display=popup&link=http%3A%2F%2Fwww.streetofwalls.com&locale=en_US&name=Career%20Networking%20powered%20by%20Street%20of%20Walls&next=http%3A%2F%2Fstatic.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D8%23cb%3Df2c657ef78%26origin%3Dhttp%253A%252F%252Fwww.streetofwalls.com%252Ff3575a615c%26domain%3Dwww.streetofwalls.com%26relation%3Dopener%26frame%3Df1ca46b43c%26result%3D%2522xxRESULTTOKENxx%2522&sdk=joey&show_error=true&to=573501273 

After url_decode() version:

https://www.facebook.com/dialog/send?display=popup&link=http://www.streetofwalls.com&locale=en_US&name=Career Networking powered by Street of Walls&next=http://static.ak.facebook.com/connect/xd_arbiter.php?version=8#cb=f2c657ef78&origin=http%3A%2F%2Fwww.streetofwalls.com%2Ff3575a615c&domain=www.streetofwalls.com&relation=opener&frame=f1ca46b43c&result=%22xxRESULTTOKENxx%22&sdk=joey&show_error=true&to=573501273

EDIT3: Part of this puzzle is solved. The times when the error occurs are the result of FB.init() not working. I've wrapped the FB.ui() in FB.getLoginStatus(function(response){ \\... } so now you can see a more useful error in the console. The open question is... WHY DOES FB.init() fail so often?

So Nitzan deserves the credit for this for his insightful comment, but here's the solution.

The error message I was getting was a result of the fact that FB.init() wasn't loading, or at least it wasn't loading in the proper order with respect to the rest of the page. I copied the code from the Facebook Developer docs and it loads asynchronously... which turns out to be kind of a big pain in the ass...

So instead of what I had, I switched to loading it the old fashioned way:

FB.init({
      appId      : '##########', // App ID
      channelUrl : '//www.xxxxxxxxxx.com/channel.php', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

This, combined with some reordering of the other scripts, seems to have resolved my problem.