'Auto post (user behalf) on facebook

I've been searching and haven't found an answer. I've a website that has a livetracking service. I already use the javascript facebook share feed so that users can post on their facebook page their and others activities.

I wonder is at the moment (I know it had been possible in the past) is possible to post on user behalf. I already have a process to get a long lived token.

The goal is to give the ability of auto post when users start/end activities, informing their followers.



Solution 1:[1]

Update from August 2018 on:

The publish_actions can not be used anymore. It is not possible to post content on a user's profile automatically anymore. Facebook suggests using the share dialog instead, which requires the user's confirmation for every post. Facebook will show a window to the user for approving the post.
Example code for using the share dialog:

FB.ui({
  method: 'share',
  href: 'https://developers.facebook.com/docs/',
}, function(response){});

The full documentation with all parameters can be found here: https://developers.facebook.com/docs/sharing/reference/share-dialog


A third party is only able to post content on behalf of a user if that user has granted permission for doing so.

The first step is to register an app, in case you haven't done it already: https://developers.facebook.com/apps/

Facebook will show a prompt to the user and ask him for giving permissions to that app. In the Facebook app, you need to ask for publish_actions permissions. This will give you an access-token to publish posts on the wall of that user in his name.

For doing so you could use the Graph API:

POST graph.facebook.com
     /{user-id}/feed?
     message={message}&
     access_token={access-token}

If you are using the JavaScript SDK, the code could look similar to this:

FB.init({ 
    appId: 'insert your appID value here', 
    cookie: true, 
    xfbml: true, 
    status: true });    

FB.api(
    "/{user-id}/feed",
    "POST",
    {
        "message": "This is a test message"
    },
    function (response) {
      if (response && !response.error) {
        /* handle the result */
      }
    }
);

Here you will find detailed information on the parameter and responses: https://developers.facebook.com/docs/graph-api/reference/v2.9/user/feed#publish

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1