Search code examples
androidfacebookfacebook-opengraphfacebook-sdk-4.0

Facebook OpenGraph Stories in Android Facebook SDK 4.0


Following this tutorial, I'm trying to implement OpenGraph stories in Android application, with Facebook SDK 4.0.
I have read a lot of Facebook documentation, and understand (in theory) how OpenGraph stories should work. But the main problem, that I can't implement it in SDK 4.0, because all code samples in tutorials and documentation are referred to the old SDK's methods. And all my attempts to make it work (change Actions, Objects) have failed.

Here's my code (according to the documentation ):

// Create an object
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
        .putString("og:type", "books.book")
        .putString("og:title", "A Game of Thrones")
        .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.")
        .putString("books:isbn", "0-553-57340-3")
        .build();

// Create an action
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
        .setActionType("books.reads")
        .putObject("books:book", object)
        .build();

// Create the content
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
        .setPreviewPropertyName("books:book")
        .setAction(action)
        .build();

ShareButton shareButton = (ShareButton)findViewById(R.id.shareButton);
shareButton.setShareContent(content);
shareButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {

    @Override
    public void onSuccess(Result result) {
        Log.i(TAG, "SHARING SUCCESS!");
    }

    @Override
    public void onError(FacebookException error) {
        Log.e(TAG, "SHARING ERROR! - " + error.getMessage());
    }

    @Override
    public void onCancel() {
        Log.w(TAG, "SHARING CANCEL!");
    }
});

And what I'm getting (with all my attempts): enter image description here

Action Requires At Least One Reference: The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: book.

Please, can you explain, how to correct use OpenGraph in FB SDK 4.0, and correct use Objects with Actions using SDK 4.0 methods.


Solution

  • There was a bug with the webview version of the dialog that's been fixed. You need to make one small change to your code, I will update our docs as well:

    // Create an action
    ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
        .setActionType("books.reads")
        .putObject("book", object)  // <--- don't namespace the object on the action
        .build();
    
    // Create the content
    ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
        .setPreviewPropertyName("book")  // <--- don't namespace the preview property
        .setAction(action)
        .build();