Android Facebook SDK 4.0 中的 Facebook OpenGraph 故事
Facebook OpenGraph Stories in Android Facebook SDK 4.0
在此 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!");
}
});
以及我得到的(我所有的尝试):
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.
请解释一下,如何在 FB SDK 4.0 中正确使用 OpenGraph,以及如何使用 SDK 4.0 方法正确使用 Objects
和 Actions
。
对话框的 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();
如果有人正在寻找它,这就是我解决问题的方法。
我还有一个问题,当我尝试使用另一个 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);
}
在此 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!");
}
});
以及我得到的(我所有的尝试):
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.
请解释一下,如何在 FB SDK 4.0 中正确使用 OpenGraph,以及如何使用 SDK 4.0 方法正确使用 Objects
和 Actions
。
对话框的 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();
如果有人正在寻找它,这就是我解决问题的方法。
我还有一个问题,当我尝试使用另一个 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);
}