user1334414
user1334414

Reputation: 11

How someone using my Facebook website app can post to their pages timeline

I have a website where businesses create content through a PHP backend system. Each time they create a new piece of content, I want it to publish to their Facebook pages timeline (not the users timeline).

I have created the authenticate code:

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'XXXXXXXXXX',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
        });
    };
    (function(d){
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
</script>
<div class="fb-login-button" scope="manage_pages">
    Login with Facebook
</div>

With manage_pages as a scope. I need to know how they can select which page they want the post to go to (if they have more than 1 page), and also how to automatically post to that pages wall when they submit the content (which is done via a PHP form).

Thanks

Upvotes: 0

Views: 382

Answers (2)

Nobita
Nobita

Reputation: 23713

They have given you the manage_pages permission right? Awesome. Here is what you need to do:

  1. Once they login, you can grasp the user access_token.
  2. With that token you will be able to fetch the Graph API for that user, and get the accounts connection. Reference -> User API (look for "Connections")
  3. That API call will give you an array of objects containing account name, access_token, category, id
  4. It's up to you to know which page to choose from those fields given. So, make your choice ;)
  5. Use the access token of your choice to post to the page. Reference -> Page API (feed)

For the other question "how to automatically post to that pages wall when they submit the content (which is done via a PHP form)." I would do some JavaScript logic that will trigger once the form is submitted and call the Facebook API with whatever content you want to POST.

Upvotes: 1

TommyBs
TommyBs

Reputation: 9646

Once users have authorised your app, you should be able to get access to their "accounts" connection. Using this you should be able to retrieve a list of their pages (which you could then use to populate a dropdown) and an access_token for the correct page. You can find more information here https://developers.facebook.com/docs/reference/api/page/ and specifically here https://developers.facebook.com/docs/reference/api/page/#feed

Note that the feed bit refers you to the main article for posts as well, but just remember you need to use the correct access_token and the page_id from above

thanks

Upvotes: 0

Related Questions