在tutorial之后,我尝试使用Facebook SDK 4.0在Android应用程序中实现OpenGraph故事。
我已经阅读了很多Facebook文档,并且(理论上)理解OpenGraph故事应该如何工作。但主要的问题是,我无法在SDK 4.0中实现它,因为教程和文档中的所有代码示例都引用了旧的SDK方法。而我所有使它成功的尝试(改变动作,对象)都失败了。
这是我的代码(根据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!");
}
});
我得到了什么(我所有的尝试):
动作需要至少一个参考:你正在尝试的动作 发布无效,因为它未指定任何引用对象。 必须至少指定以下属性之一:book。
请解释一下,如何在FB SDK 4.0中正确使用OpenGraph,并使用SDK 4.0方法正确使用Objects
和Actions
。
答案 0 :(得分:2)
对话框的webview版本有一个错误已得到修复。您需要对代码进行一处小改动,我也会更新我们的文档:
// 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();
答案 1 :(得分:0)
万一有人在寻找它,这就是我解决问题的方式。
还有一个问题,当我尝试使用另一个CallbackManager.Factory.create();
时,我的FacebookCallback<Sharer.Result>
未被通知,我对所有Facebook动作只使用了一个实例。
public static void faceBookShareDialog(KoboActivity activity, Content content, Bitmap imageToShare,
String textToShare) {
ShareHashtag shareHashtag = new ShareHashtag.Builder().setHashtag(HASH_TAG_KOBO).build();
SharePhoto photo;
if (!isFacebookInstalled(activity)) {
ImageConfig imageConfig = Application.getAppComponent().imageConfigFactory().create(content.getImageId(), ImageType.Cover);
String imageUrl = imageConfig.getImageRequestURL();
photo = new SharePhoto.Builder()
.setImageUrl(Uri.parse(imageUrl))
.build();
} else {
photo = new SharePhoto.Builder()
.setBitmap(imageToShare)
.build();
}
// Create an object
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
.putString("og:type", "books.book")
.putString("og:title", content.getTitle())
.putString("og:description", content.getDescription())
.putString("books:isbn", "0-553-57340-3")
.putPhoto("og:image", photo)
.build();
// Create an action
ShareOpenGraphAction.Builder actionBuilder = new ShareOpenGraphAction.Builder()
.setActionType("books.quotes")
.putString("body", textToShare)
.putObject("book", object);
// Create the content
String contentURL = generateFacebookShareUrl(content.getId(), content.getType(), CreatePhotoQuoteActivity.CAMPAIGN);
ShareOpenGraphContent contentGraph = new ShareOpenGraphContent.Builder()
.setPreviewPropertyName("body")
.setShareHashtag(shareHashtag)
.setAction(actionBuilder.build())
.setContentUrl(Uri.parse(contentURL))
.build();
FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
@Override
public void onCancel() {
Log.i("FBShare", "fb SHARE canceled");
}
@Override
public void onError(FacebookException error) {
Log.e("FBShare", "fb SHARE error");
error.printStackTrace();
}
@Override
public void onSuccess(Sharer.Result result) {
String postId = result.getPostId();
Log.i("FBShare", "fb SHARE success ");
if (postId != null) {
// record successful FB share
Log.i(TAG, "post id: " + postId);
}
}
};
SharePhotoContent photoContent = new SharePhotoContent.Builder()
.addPhoto(photo)
.setShareHashtag(shareHashtag)
.build();
ShareDialog shareDialog = new ShareDialog(activity);
shareDialog.registerCallback(activity.getCallbackManager(), shareCallback);
shareDialog.show(photoContent);
}