facebookfacebook-graph-apifacebook-javascript-sdkfacebook-opengraphfb.ui

Dynamically generate description for explicitly shared open graph story through FB.ui Share Dialog


Of all of the research I've done on this topic, 90% of it is outdated, which brings me to believe there have been recent changes to the Facebook JS SDK regarding explicitly sharing Open Graph stories. I have the following code:

function postToFB(desc){
FB.ui({
    method: 'share_open_graph',
    action_type: 'mynamespace:myaction',
    action_properties: JSON.stringify({
        myobject: "myobjectid"
    })
}, function(response){});
}

The problem with this is that it will attempt to share myobject with only the settings I've set up in the Facebook Open Graph section of my app settings. I would like to use the same object, but change the description with a dynamic 'desc' variable each time it's shared. Does anyone know how to accomplish this? I can statically set the Caption in my Facebook Open Graph section of my app settings, but I need to dynamically set it every time I share it instead.

I've searched through the Facebook JS SDK for additional key/value pairs that can be added to action_properties, but the SDK information is very limited in this regard.

UPDATE

I've modified the code to include 2 calls, one to create the object and the second to publish the story with the new object's information. Here's the code:

function postToFB(desc){
FB.api(
    'me/objects/mynamespace:myobject',
    'post',
    {
    object: {
        "app_id": myappid,
        "type": "mynamespace:myobject",
        "url": "myurl",
        "title": "mytitle",
        "image": "myimgurl",
        "description": desc
        }
    },
    function(response) {
        console.log(response);
        FB.ui({
            method: 'share_open_graph',
            action_type: 'mynamespace:myaction',
            action_properties: JSON.stringify({
                myobject: response.id
        })
    }, function(r){});
});
}

However, according to the comments on this question: Facebook object API - duplicate objects created a Facebook staff member said this approach will not be approved by Facebook because one action by a user cannot produce two posts, which this code now does. One will post the object to the Facebook user's activity log, and one will post it to the activity log and the news feed.

It would be nice if I could just make one call and stipulate what the myobject description should be from the FB.ui function itself. Does anyone know how to do this?


Solution

  • I figured it out. The main problem I was having is I was posting to me/objects (which would post an event to the user's activity log) when I should have been posting to app/objects (which would create the object but not post an event). Since posting to app/anything requires an access token, I had to change the initial call to happen on the server side in PHP:

    $request = new FacebookRequest(  
    null,  
    'POST',  
    '/app/objects/mynamespace:myobject', 
    array(
            'access_token' => 'myaccesstoken',
        'object' => json_encode(array(
            'app_id' => myappid,
                'url' => 'myurl',
                'title' => 'mytitle',
                'image' => 'myimg',
                'description' => 'mydesc'
            ))
    )
    );
    $response = $request->execute();
    $obj = $response->getGraphObject();
    echo $obj->getProperty('id');
    

    From there I could use the JS script to post the object with the new ID to the user's feed.

    Hope that prevents you from spending 2 and a half days trying to figure it out on your own like I did.