Search code examples
javaandroidfacebookfacebook-sdk-4.x

Facebook sharing in Libgdx with 4.x Facebook SDK


I have problem with sharing something with FacebookSDK in a LibGDX Android application. If I click the share button and share something I get back Sharer.Result null (so the post is not shared, but in reality it is!), which is the same result I get when I cancel sharing.

Here is my code:

 Android Manifest
<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id"/>


    <provider android:authorities="com.facebook.app.FacebookContentProviderMYFACEBOOKID"
              android:name="com.facebook.FacebookContentProvider"
              android:exported="true"/>

    <activity android:name="com.facebook.FacebookActivity"
              android:configChanges=
                  "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:label="@string/app_name" />
</application>

AndroidLauncher.java (activity that starts the whole app):

  @Override
protected void onResume() {
    FacebookSdk.sdkInitialize(this);
    super.onResume();
    AppEventsLogger.activateApp(this);
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebookPluginAndroid.getCallbackManager().onActivityResult(requestCode, resultCode, data);
}

facebookPluginAndroid is my class that have CallBackManager:

this.callbackManager = CallbackManager.Factory.create();

And the share() function (that launches when I press the share button):

 public void share(final FacebookPluginListener facebookPluginListener){
    shareDialog = new ShareDialog(context);

    final ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("https://www.facebook.com/SOME URL TO MY SITE"))
            .build();
    if(shareDialog.canShow(content)){
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                System.out.println("share SHARED " + result.getPostId());
                if(result.getPostId()!=null)
                    facebookPluginListener.getFacebookShareListener().onShareSuccess();
            }

            @Override
            public void onCancel() {
                System.out.println("share CANCEL");
            }

            @Override
            public void onError(FacebookException e) {
                System.out.println("share ERROR");
            }


        },121);

        Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
                shareDialog.show(content);
            }
        });
    }
}

What I am doing wrong? What must I do to properly share content with FacebookSDK?

@EDIT: FacebookPluginAndroid.java:

public class FacebookPluginAndroid {
private Activity context;
private ShareDialog shareDialog;
private CallbackManager callbackManager;
private LikeDialog likeDialog;

public FacebookPluginAndroid(Activity context) {
    this.context = context;
    this.callbackManager = CallbackManager.Factory.create();
}

public void share(final FacebookPluginListener facebookPluginListener) {


    shareDialog = new ShareDialog(context);
    shareDialog.setShouldFailOnDataError(true);
    final ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("https://www.facebook.com/SOME URL"))
            .build();
    if (shareDialog.canShow(content)) {
        shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
            @Override
            public void onSuccess(Sharer.Result result) {
                System.out.println("share SHARED " + result.getPostId());
                if (result.getPostId() != null)
                    facebookPluginListener.getFacebookShareListener().onShareSuccess();
            }

            @Override
            public void onCancel() {
                System.out.println("share CANCEL");
            }

            @Override
            public void onError(FacebookException e) {
                System.out.println("share ERROR");
            }


        }, 121);

        Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
                shareDialog.show(content);
            }
        });
    }
}

public void like(final FacebookPluginListener facebookPluginListener) {
    likeDialog = new LikeDialog(context);
    final LikeContent content = new LikeContent.Builder()
            .setObjectId("https://www.facebook.com/SOMEURL")
            .build();


    if (likeDialog.canShow(content)) {
        likeDialog.registerCallback(callbackManager, new FacebookCallback<LikeDialog.Result>() {
            @Override
            public void onSuccess(LikeDialog.Result result) {
                System.out.println("like success! android LIKE" + result.getData());

                if (result.getData().getBoolean("object_is_liked"))
                    facebookPluginListener.getFacebookLikeListener().onLikeSuccess();
            }

            @Override
            public void onCancel() {
                System.out.println("like cancel! android LIKE");
            }

            @Override
            public void onError(FacebookException e) {
                System.out.println("like error! android LIKE");
            }


        }, 123);

        Gdx.app.postRunnable(new Runnable() {
            @Override
            public void run() {
                likeDialog.show(content);
            }
        });
    }
}

public CallbackManager getCallbackManager() {
    return callbackManager;
}

}


Solution

  • Post_id response only available if the user is logged into your app using Facebook and has granted publish_actions. If present, this is the ID of the published Open Graph story.

    U need add loginButton.setPublishPermissions(Arrays.asList("publish_actions"));