FB.ui feed对话框需要redirect_uri,对话框不会关闭

时间:2012-01-20 21:23:00

标签: javascript facebook sdk dialog fb.ui

我正在尝试使用JS SDK的FB.ui方法打开一个提要对话框,并在用户共享后关闭它。我的问题是feed对话框需要redirect_uri,即使文档说它不必定义,弹出窗口会重定向到那里并且不会像回调函数那样关闭。

这是我的代码,附在提交点击事件上:

    FB.ui (
        {
            method: 'feed',
            name: 'xxx!',
            link: 'link to FB tab',
            picture: 'jpg',
            caption: 'xxx',
            actions: {name:'xxx',link:'url'},
            ref: 'xxx',
            redirect_uri: 'link to FB tab'
        },
        function(response) {
            self.close();
        }
    );

如果我不使用redirect_uri,弹出窗口会打开,但它只是说FB应用程序有错误,请再试一次。

3 个答案:

答案 0 :(得分:2)

这似乎是Facebook的JavaScript SDK中的一个已知变化:http://developers.facebook.com/bugs/302946973066993

  

使用Facebook JavaScript API时,除非在params对象中提供'redirect_uri'属性,否则调用FB.ui将失败 - 这种行为是意外的,因为:

     

1。)文档说明'redirect_uri'将自动附加到大多数SDK [1] - 之前JavaScript SDK提供了一个关闭Lightbox iFrame的SDK。 2.)添加redirect_uri参数会导致Facebook Lightbox iFrame重定向,从而阻止用户关闭它。   3.)之前不需要redirect_uri参数。

这是我习惯并且一直试图复制的行为。 FB dev报告说这是“按设计”。

答案 1 :(得分:1)

在花了一整天的时间研究这个问题后,我有一个非常好的解决方案,我想分享。我没有使用带有FB.ui()的SDK,而是发现我可以通过手动打开自己的弹出窗口https://www.facebook.com/dialog/feed来完全避免它。以这种方式执行时,redirect_uri按预期工作,您只需重定向到关闭弹出窗口的HTML文件即可。无论用户点击共享还是取消,弹出窗口都会按预期关闭。

我不相信此代码有任何妥协,如果有的话,它比实际的SDK更容易使用。

我的Javascript代码(可以保存为FacebookFeedDialog.js)如下所示:

/* by Steven Yang, Feb 2015, originally for www.mathscore.com.  This code is free for anybody to use as long as you include this comment.  */
function FacebookFeedDialog(appID, linkTarget, redirectTarget) {
  this.mParams = {
    app_id: appID,
    link: linkTarget,
    redirect_uri: redirectTarget,
    display: "popup"
  }
};

/* Common params include:
   name - the title that appears in bold font
   description - the text that appears below the title
   picture - complete URL path to the image on the left of the dialog
   caption - replaces the link text
*/
FacebookFeedDialog.prototype.addParam = function(key, value) {
  this.mParams[key] = value;
};

FacebookFeedDialog.prototype.open = function() {

  var url = 'https://www.facebook.com/dialog/feed?' + encodeCGIArgs(this.mParams);
  popup(url, 'feedDialog', 700, 400);
};

/* Takes a param object like this:
   { arg1: "value1", arg2: "value2" }
   and converts into CGI args like this:
   arg1=value1&arg2=value2

   The values and args will be properly URI encoded
*/
function encodeCGIArgs(paramObject) {

  var result = '';

  for (var key in paramObject) {
    if (result)
      result += '&';
    result += encodeURIComponent(key) + '=' + encodeURIComponent(paramObject[key]);
  }

  return result;
}

function popup(mylink,windowname,width,height) {
  if (!window.focus) return;
  var href;
  if (typeof(mylink) == 'string')
    href=mylink;
  else
    href=mylink.href;
  if (!windowname)
    windowname='mywindow';
  if (!width)
    width=600;
  if (!height)
    height=350;
  window.open(href, windowname, 'resizable=yes,width='+width+',height='+height+',scrollbars=yes');
}

以下是使用上述Javascript代码的示例HTML文件:

<HTML>
<BODY>
<SCRIPT type="text/javascript" src="FacebookFeedDialog.js"></SCRIPT>
<SCRIPT>
var dialog = new FacebookFeedDialog(yourAppIDGoesHere,yourDestinationURLGoesHere,yourCloseWindowURLGoesHere);
dialog.addParam('name','This is my title');
dialog.addParam('description','This is the description');
dialog.addParam('picture',yourImageURLGoesHere);
dialog.addParam('caption','This is the caption');
</SCRIPT>

<A href="javascript:dialog.open()">Open facebook dialog</A>
</BODY>
</HTML>

您的closeWindow html文件可能如下所示:

<SCRIPT>
window.close();
</SCRIPT>

答案 2 :(得分:0)

嗯,我看到的文档说它是必需的,必须定义....

  

redirect_uri

     

用户单击对话框上的按钮后重定向到的URL。   必需,但由大多数SDK自动指定。