Search code examples
facebookfacebook-graph-apipublish

How to find out who has published a post using my OG action


Hoping someone can point me in the right direction.

I have an open graph action that has finally been approved and is active on my app now. Is there any way to track who publishes a post using my OG action from within my app?

Here is the code I'm using for my action:

<script type="text/javascript">
  function postEndorse()
  {
      FB.api(
        '/me/namespace:endorse',
        'post',
        { photo: '<?php the_permalink(); ?>' },
        function(response) {
           if (!response || response.error) {
              alert('Error Occurred' + response + " " + response.error);
           } else {
              alert('Thank you!');
           }
        });
  }
  </script>

I am requesting the publish_actions permissions when people log in.

Thanks


Solution

  • The response in your JavaScript code above has the id of the action.

    {
      id: “{action-instance-id}”
    }
    

    You need to store your actions via database or otherwise. Once you have that you can make a call against it

    /action-instance-id?fields=from.fields(id)
    

    Which will give a response such as

    {
      "from": {
        "id": "{user-id}"
      }, 
      "id": "{action-instance-id}"
    }
    

    There are many ways to store this data, one way would be AJAX to PHP/MySQL.
    I suggest reading up on it. The following hasn't been tested, just gives an idea of how to send the data across. Think of it as pseudocode

    Add jQuery to the <head>

    <script src="jquery.js"></script>
    

    Send the id of the user, like

    <script type="text/javascript">
      function postEndorse()
      {
          FB.api(
            '/me/namespace:endorse',
            'post',
            { photo: '<?php the_permalink(); ?>' },
            function(response) {
               if (!response || response.error) {
                  alert('Error Occurred' + response + " " + response.error);
               } else {
                  FB.api(
                    response.id + "?fields=from.fields(id)",
                     function(resp) {
                         if (!resp || resp.error) {
                            alert('Error Occurred' + resp + " " + resp.error);
                          } else {
                            $.ajax({
                               type: "POST",
                               url: "fbdata.php",
                               data: { id: resp.from.id, actionid: resp.id }
                            }).done(function( msg ) {
                               alert( "Data Saved: " + msg );
                            });
                         }
                  });
               }
            });
      }
      </script>
    

    Then in fbdata.php something like

    <?php
    
    include("database.inc.php");
    mysql_connect($server,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database"); 
    
    $id = mysql_real_escape_string($_POST['id']);
    $actionid = mysql_real_escape_string($_POST['actionid']);
    
    $database_entry = "INSERT INTO Actions (Id, Action) VALUES ('$id', '$actionid') ;
    mysql_query($database_ntry) or die(mysql_error());
    
    
    mysql_close();
    ?>
    

    References that you should read