2

I have the following code to share to a facebook wall, but it doesn't give the option to share to any managed pages the user has admin rights to (i tried testing with my own account) can anyone help me with this? Also i want to post in the future according to a date set in feed if possible.

<script>
window.fbAsyncInit = function() {
  FB.init({appId: '475259649152397',
    channelUrl : '<?php get_theme_root();?>/inc/facebook-javascript-sdk/channel.php',
    status: true, 
    cookie: true,
    xfbml: true
  });
};

function Login()    
{
   FB.login(function(response) 
   {
       if (response.authResponse) 
       {
            getUserInfo();
       } 
       else 
       {
         console.log('User cancelled login or did not fully authorize.');
       }
   },{scope: 'manage_pages'});
}

(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>

<script type="text/javascript">
jQuery(document).ready(function(){
   jQuery('#share_button').click(function(e){
     e.preventDefault();

     FB.ui({
       method: 'feed',
       name: 'This is the content of the "name" field.',
       link: ' http://example.com/',
       picture: 'http://myface.gif',
       caption: 'insightful thought provoking caption.',
       description: 'interresting".',
       message: ''
     });
   });
});
</script>

It works just fine at sharing to the users wall but no option to share to the managed pages

thanks in advance!

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
David
  • 5,897
  • 3
  • 24
  • 43

1 Answers1

1

option to share to any managed pages the user has admin rights to

Simply, give the PAGE_ID to the to parameter. Just like-

FB.ui({
   method: 'feed',
   name: 'This is the content of the "name" field.',
   link: ' http://example.com/',
   picture: 'http://myface.gif',
   caption: 'insightful thought provoking caption.',
   description: 'interresting".',
   message: '',
   to: MY_PAGE_ID
 });

i want to post in the future

You can use the page access token if you want to post to your page at any later time, the page access token could be extended to never expiring. What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server


To get the pages managed by you, query for-

/me/accounts?fields=id,name

, you'll get an array of pages you are managing. Live Demo

Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • Hi Mate, Thanks for this. I can see this would work but I am running into issues with the page_id as i don't know what the page_id will be. I tried requesting the page_id from the Graph API 'function getUserInfo(){ FB.api('/me', function(response) { var $page_id = response.PAGE_ID };' i think it needs to be platform/something does this look like im on the right track? – David Sep 11 '13 at 00:31
  • Hold on! You know which page you want to post to, but dont know the page id? How's this possible! What are you saying? Which pages are you trying to post to? How in the world you are trying to get the page id from `/me`!! lol :P pls clarify, u'r cofused! I think you are looking for your own pages pls chk the update – Sahil Mittal Sep 11 '13 at 05:47
  • You can not just request `/me` from the API and expect to get a page id back. There is no direct connection between a user and one specific page. – CBroe Sep 11 '13 at 08:05
  • Hi Shadowfax, sorry if the question was a bit confusing.....basically i am creating a share button for a wordpress post submitted from the frontend where the visitor can share their post onto their business page to get a discount off the cost of posting so as the user will be different everytime the id will change...........the update was bang on tho, thanks for that :) – David Sep 11 '13 at 09:44
  • adding `to: MY_PAGE_ID` is not working for me? Did you add any other things ? – kittu Apr 07 '17 at 19:33