Reputation: 3177
I have an app on Facebook (we'll call it mygame
), connected with an Android game I'm developing.
In this mobile app, I want to let users to share their scores after a match. For this, I've considered the use of Open Graph stories, actions and objects.
The first thing I've done is to create an action, "Play" and an object "Match", this one with an integer property named "score". After that, I've created my first story in the form "Play a Match" using the relative button "Add custom story".
First problem: all the generated examples don't show a preview, instead a red message appears, Unable to Generate Story
.
This problem was previously treated on Stackoverflow, and the cause was related to the fact that "facebook has no examples of the story to render". That should not be true in my case, because if I open the Object Browser I can see two auto-generated Match objects. Does Facebook try to generate example stories from those records? If not (or even true), what's the problem?
Second problem SOLVED, SEE ANSWERS: on Android, simply, I can't test this story due to an exception that says com.facebook.FacebookException: Failed to generate preview for user.
.
The method I've used to show a Facebook Dialog with the details of this "played match" is the one described here: https://developers.facebook.com/docs/android/open-graph#sharedialog-setup
In my app, the code is the following:
OpenGraphObject setObj = OpenGraphObject.Factory.createForPost("mygame:match");
setObj.setProperty("score", set.getThisUserScore());
setObj.setProperty("title", set.getType().getDisplayString());
setObj.setProperty("url", "http://www.mygame.com");
setObj.setProperty("description", "Can you beat me?");
OpenGraphAction action = GraphObject.Factory.create(OpenGraphAction.class);
action.setType("mygame:play");
action.setProperty("match", setObj);
FacebookDialog shareDialog = new FacebookDialog.OpenGraphActionDialogBuilder(activity, action, "match").build();
activity.getUiHelper().trackPendingDialogCall(shareDialog.present());
Once called, the Dialog appears on my device, but it closes itself after one second, throwing the exception.
I really can't focus on where the problem is (I'm trying to implement the simplest way of sharing stories). Are these two problems connected?
Upvotes: 4
Views: 2826
Reputation: 5121
I use this code to publish on wall for multiple object properties.
private void publishPhoto(String imageURL) {
Log.d("FACEBOOK", "Post to Facebook!");
try {
JSONObject attachment = new JSONObject();
attachment.put("message",text);
attachment.put("name", "MyGreatAndroidAppTest");
attachment.put("href", "http://stackoverflow.com/users/909317/sunny");
attachment.put("description","Test Test TEst");
JSONObject media = new JSONObject();
media.put("type", "image");
media.put("src", imageURL);
media.put("href",imageURL);
attachment.put("media", new JSONArray().put(media));
JSONObject properties = new JSONObject();
JSONObject prop1 = new JSONObject();
prop1.put("text", "Text or captionText to Post");
prop1.put("href", imageURL);
properties.put(text, prop1);
// u can make any number of prop object and put on "properties" for ex: //prop2,prop3
attachment.put("properties", properties);
Log.d("FACEBOOK", attachment.toString());
Bundle params = new Bundle();
params.putString("attachment", attachment.toString());
facebook.dialog(MyProjectActivity.this, "stream.publish", params, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.d("FACEBOOK", "Dialog Success! post_id=" + postId);
Toast.makeText(MyProjectActivity.this, "Successfully shared on Facebook!", Toast.LENGTH_LONG).show();
} else {
Log.d("FACEBOOK", "No wall post made");
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
} catch (JSONException e) {
Log.e("FACEBOOK", e.getLocalizedMessage(), e);
}
}
Upvotes: 1
Reputation: 3177
I've solved the second problem about the FacebookException
!
There's a wrong line:
setObj.setProperty("score", set.getThisUserScore());
Being "score" a custom property, the line must be replaced by:
setObj.getData().setProperty("score", set.getThisUserScore());
Note that I had to search deeply in the documentation to find this...
Upvotes: 0