Search code examples
facebookfacebook-graph-apifacebook-javascript-sdkfacebook-timeline

Deleting action on Timeline using JS SDK


I have an 'Add To Timeline' button and I can already add posts to the timeline using another function, but now I want to be able to obtain the requestID of an action so that I can delete it from another javascript function I have. I been searching everywhere but can't find a good example so that I can learn how to obtain the ID, I'm still confused on how to do it. This is the code:

       <script type="text/javascript">
       function deleteRead(requestId)
       {        
       FB.api(
             requestId,
               'delete',
                function(response) {
                 if (!response || response.error) {
                        alert('error...');
                 } else {
                     alert('Successfully Deleted!');
                     }
                  });
             }</script>

Adding the Action ID number of a post directly works and I'm able to delete the post on my timeline. But I have to make it where it automatically obtains the id for each post.

Do I have to use the PHP SDK in order to obtain the requestID? This is the other code I am using:

            <div id="fb-root"></div>
             <script type="text/javascript">
              window.fbAsyncInit = function() {

                 FB.init({
                 appId      : 'I-have-my-app-ID-here', // App ID
                 status     : true, // check login status
                 cookie     : true, // enable cookies to allow the server to access the session
                 xfbml      : true  // parse XFBML
                 }); 

             };

              // Load the SDK Asynchronously
             (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>

Even better, Is there a way where I can grab the requestID through PHP and pass it into a variable? depending on the post (if it's added to timeline), so that I can replace it with "requestID" to something like:

             <?php echo $requestID; ?>

This way I can use the php code inside of my javascript where the requestID is and plus incase you want to use that same requestID that is obtained for something else in that same page. Example: if the post is added to Timeline it will show it's request ID number on that post page, incase you want to show the delete button only if its added to timeline, if it's not added, then don't show the delete button using a conditional statement.


Solution

  • There are two ways of doing this:

    • Store the IDs in a database for each action. This is not ideas as actions can be deleted from Facebook and can make it out of sync with your database.
    • Query the user's actions in real-time and use that to show a list of actions to delete. This is preferred.

    Sample code:

    // get activity from Facebook
    $actions = $facebook->api('/me/' . $action_id );
    // get ID of last action
    echo $actions[0]['id'] );
    

    You can then grab the ID of the post from above (e.g. $actions[0]['id']) and pass it to your JavaScript function or create a PHP function to delete the action. Optionally, you can loop through $actions to search if a particular action already exists, matching by URL, e.g.

    if ( $actions[0]['data']['article']['url'] == $current_url ) {
        ...
    }