Question

I have a site that needs to post to a user's Facebook wall right away, and at some later point (which can be anywhere from 1 hour to 3 days later, at any time day or night), post another message to the user's wall.

To do this, I explain clearly to the user what's going to happen, then I connect to their Facebook account asking for publishing permissions (generating the permissions dialog for the user). At that point I can post the 1st message to their wall immediately. Later, when it's time to post the 2nd message to their wall (which again could be any time, the user is long gone), my server will receive a message. I will tag this message with some identifying information (for example, the user's Facebook user ID number) so I know whose wall to post to.

My question is, what specifically do I need to save in my local DB about their Facebook connection, so I can make the 2nd post (at some undefined point in the future, when the user is gone)?

I'm using the FB JS API and using FB.login to log into Facebook with the permissions I need, etc. The server side code is in PHP, so after JS retrieves them, I can send whatever credentials I need to save over to PHP code (via Ajax) to store in the local database.

For publishing the 2nd message, it will be all PHP (using the Facebook PHP SDK) since there is no user at the console/no Javascript involved.

I believe the answer I'm seeking should look something like:

  1. When you initially connect to Facebook and the user grants permission to publish, Facebook will return xyz credential data. You need to store that info in your local database. Use the FB user ID as the key.

  2. When you are ready to publish the 2nd message and your PHP script kicks in, retrieve xyz information from the database (using the FB user ID), then use the xyz call in the FB PHP SDK to actually publish what you want to post on their wall.

Thanks!

Was it helpful?

Solution

The steps are quite straight-forward. Let me explain-

Publishing a post, requires an access token; and once a user has authorized your application to post on your behalf, you can post on his behalf using the app access token. That it! So here are the steps-

  1. When user authorized your app successfully to post on his behalf, just save his/her facebook id in your database.
  2. Use the access token which is nothing but app-id|app-secret(beware, dont expose this on client side ever), to publish on his/her wall. Just like-

    $response = $facebook->api(
      "/me/feed",
      "POST",
      array (
         'message' => 'This is a test message', 
         ....
         'access_token' => "app-access-token"
      )
    );
    

But things to consider:

  1. While authorizing, the user may/may not give you the permission to post, you can check that with /me/permissions just after the authorization step; so you should act accordingly.

  2. At any time later, the user can delete your app, or remove the permissions of your app from the app settings. In that case your posting script while give you the authorization error; in that case too handle appropriately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top