如何通过Facebook SDK及JavaScript实现向多位好友分享动态?
Hey there! Let's figure out how to expand your Facebook share feature to work with multiple friends. Your existing code works great for sharing to your own timeline, so let's adjust it to support selecting or specifying multiple recipients.
First, let's clarify two key Facebook dialogs that fit your needs:
- Feed Dialog: Shares content to your personal timeline (you can tag multiple friends here)
- Send Dialog: Sends content directly to friends' Messenger inboxes (this is the better fit if you want to send the content directly to multiple contacts, not just tag them in your post)
1. Modify Your Feed Dialog to Support Multiple Friends
If you want to share to your timeline and tag multiple friends, you can add the to parameter (accepts a comma-separated string of friend IDs) and enable multi_select: true to let users pick friends from a list. Here's the updated code:
<!DOCTYPE HTML> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> </head> <body> <div id="fb-root"></div> <img id="share_button" src="img/share_button.png"> <script type="text/javascript"> $(document).ready(function(){ $('#share_button').click(function(e){ e.preventDefault(); FB.ui({ method: 'feed', name: 'HyperArts Blog', link: 'http://hyperarts.com/blog', picture: 'http://www.hyperarts.com/_img/TabPress-LOGO-Home.png', caption: 'I love HyperArts tutorials', description: 'The HyperArts Blog provides tutorials for all things Facebook', message: '', multi_select: true, // Lets users pick multiple friends to tag to: '' // Leave empty to let users select, or add comma-separated friend IDs (e.g., '12345,67890') }, function(response) { // Handle the response after user interacts with the dialog if (response && response.post_id) { alert('Post shared successfully!'); } else { alert('Share canceled or failed.'); } }); }); }); </script> <script> window.fbAsyncInit = function() { FB.init({ appId: '193124077909182', status: true, cookie: true, xfbml: true }); }; (function() { var e = document.createElement('script'); e.src = 'https://connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); </script> </body> </html>
2. Use Send Dialog for Direct Sharing to Multiple Friends
If your goal is to send the content directly to friends' inboxes (not just tag them in your post), the Send Dialog is the right tool. It lets users select multiple friends/groups to send the link directly:
<!DOCTYPE HTML> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> </head> <body> <div id="fb-root"></div> <img id="share_button" src="img/share_button.png"> <script type="text/javascript"> $(document).ready(function(){ $('#share_button').click(function(e){ e.preventDefault(); FB.ui({ method: 'send', link: 'http://hyperarts.com/blog', message: 'Check out this awesome Facebook tutorial from HyperArts!', // Custom message for recipients to: '' // Leave empty to let users select, or add comma-separated friend IDs }, function(response) { if (response) { alert('Message sent to your friends!'); } else { alert('Send canceled or failed.'); } }); }); }); </script> <script> window.fbAsyncInit = function() { FB.init({ appId: '193124077909182', status: true, cookie: true, xfbml: true }); }; (function() { var e = document.createElement('script'); e.src = 'https://connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); </script> </body> </html>
3. Critical Notes & Facebook Policy Rules
- No Auto-Sharing: Facebook strictly prohibits automatically sharing content to multiple friends without explicit user consent. Always let users select recipients manually (using
multi_selector the default dialog picker). - Friend ID Access: If you want to pre-fill friend IDs, your app needs the
user_friendspermission. Keep in mind this permission only returns friends who also use your app (per Facebook's current policies). - Avoid Spam: Don't use these features to send unsolicited content—this will get your app restricted or banned.
内容的提问来源于stack exchange,提问作者Umar Safeer

