Search code examples
androidfacebook-graph-apifacebook-opengraphfacebook-android-sdkfacebook-share

Android: OpenGraph Stories sharing in Facebook with SDK 4.0


I am trying to implement achievements for sharing to facebook when the player unlocks a badge. Have made an object using the object browser in facebook developer console. I made Action-types and object-types and made a custom story. Now i am stuck trying to share the story to facebook. The documentation given by facebook is inadequate. Even the sample code given by facebook uses v3.x

Sample code given by facebook is given below. Couldnt find any good documentation.

Code for Object

Bundle params = new Bundle();
Request request = new Request(
    Session.getActiveSession(),
    "me/objects/enguru_app:badge",
    params,
    HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response

Code for Action

Bundle params = new Bundle();
params.putString("badge", "http://samples.ogp.me/1114467558579559");
Request request = new Request(
    Session.getActiveSession(),
    "me/enguru_app:unlocked",
    params,
    HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response

Solution

  • At Last figured out my own issue.

    Here is the solution:

    ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
                .putString("og:type", "enguru_app:badge")
                .putString("og:title", "Unlocked Newbie Badge")
                .putString("og:url","xxxx")
                .putString("og:image","xxx")
                .putString("game:points", "10")
                .putString("fb:app_id", "xxx")
                .putString("og:description",
                        "We are rocking. Come and Play with us").build();
        // Create an action
        ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
                .setActionType("enguru_app:unlocked")
                .putObject("badge", object).build();
        // Create the content
        ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
                .setPreviewPropertyName("badge").setAction(action)
                .build();
    
        ShareDialog.show(Profile.this, content);
    

    I hope this will help someone who is going through the same issue.