0

I'm sure there is a relatively simple way to solve this problem, but I could use some help. I have a landing page that accepts a user's email address and generates a unique URL that they can use to refer friends. I use AJAX to bind to the form and render the user's unique referral link as well as some social sharing mechanisms. For these social sharing mechanisms (Tweet, Share, etc.), I need to pass the user's dynamically generated unique referral URL to the Facebook postToFeed() script.

I used to use fb:share-button, but it seems that's now deprecated in favor of the like and posttofeed actions. In past applications and with other social network sharing functions like Twitter this was trivial because the those buttons use a href attribute.

<a href="#" id="twitter">Tweet</a>

which I can then update on the appropriate trigger event using JS or jQuery

$('#twitter').attr('href', data.link);

However, with postToFeed(), you need to pass in the information to be shared in a script (see below).

Can anyone help me figured out how I dynamically add the appropriate redirect_uri below after it has been created when the user submits their email address?

Thanks a bunch!

        FB.init({appId: "", status: true, cookie: true});
        function postToFeed() {
          var obj = {
            method: 'feed',
            redirect_uri: '',
            link: '',
            picture: '',
            name: '',
            caption: '',
            description: ''
          };

          function callback(response) {
            document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
          }

          FB.ui(obj, callback);
        }
4

2 回答 2

2

You could place the obj variable outside of the postToFeed method:

var postToFeedObj = {
    method: 'feed',
    redirect_uri: '',
    link: '',
    picture: '',
    name: '',
    caption: '',
    description: ''
};

function postToFeed() {

    function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

    FB.ui(postToFeedObj, callback);
}

and change the object properties for the link to your needs. There is also a way to do it with a direct link, but in this case you don't have a callback:

https://www.facebook.com/dialog/feed?
  app_id=YOURAPPID&
  link=YOURLINK&
  picture=YOURPICTURE&
  name=THE%20NAME&
  caption=THE%20CAPTION&
  description=THE%20DESCRIPTON&
  redirect_uri=THEREDIRECTLINK
于 2013-01-28T00:53:44.763 回答
1

The way I do it is:

Add onclick method to your href:

<a href="#" onclick = 'postToFeed("<?= $uniqueURL;?>")' id="facebook">Share</a>

And then javascript looks like:

FB.init({appId: "125980477501398", status: true, cookie: true});
    function postToFeed(url) {
      /* You said you wanted it to be redirect_uri, so you put 
       * the url parameter there
       */
       var obj = {
        method: 'feed',
        redirect_uri: url,
        link: '', 
        picture: '',
        name: '',
        caption: '',
        description: ''
      };

      function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
      }

      FB.ui(obj, callback);
    }

Hope this helps!

于 2013-01-28T01:11:26.093 回答