FB.ui feed dialog requires redirect_uri, dialog does not close

后端 未结 3 1170
一向
一向 2021-02-09 23:15

I am trying to open a feed dialog using the JS SDK\'s FB.ui method and have it close after the user shares. My problem is the feed dialog is requiring a redirect_uri even though

3条回答
  •  忘掉有多难
    2021-02-09 23:57

    After spending a whole day working on this problem, I have a very good solution that I'd like to share. Instead of using the SDK with FB.ui(), I have discovered that I can avoid it entirely by manually opening my own popup to https://www.facebook.com/dialog/feed. When doing it this way, redirect_uri works as expected, and you can just redirect to an HTML file that closes the popup window. Whether the user clicks on share or cancel, the popup will close as expected.

    I don't believe there are any compromises with this code, and if anything, it is much easier to use than the actual SDK.

    My Javascript code (which you can save as FacebookFeedDialog.js) looks like this:

    /* 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');
    }
    

    Here's a sample HTML file that uses the Javascript code above:

    
    
    
    
    
    Open facebook dialog
    
    
    

    Your closeWindow html file can look like this:

    
    

提交回复
热议问题