I'm currently working on sharing a place on facebook.
Here's my code
// Create an object
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
.putString("og:type", "anewplace")
.putString("og:title", "Paris")
.putDouble("place:location:latitude", 10.7672788)
.putDouble("place:location:longitude", 106.6877127)
.build();
// Create an action
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
.setActionType("come")
.putObject("anewplace", object)
.build();
// Create the content
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
.setPreviewPropertyName("anewplace")
.setAction(action)
.build();
ShareButton shareButton = new ShareButton(this);
shareButton.setShareContent(content);
shareButton.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.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!");
}
});
shareButton.performClick();
I got an error : Failed to generate preview for user
I followed the documentation to create a custom story
Please help :) thanks
Thanh
i found the way :)
for the action, it should be this format "app_name:action_name"
and for the object, it should be "app_name:object_name"
Related
I am taking a photo using an Android phone, and I want to post it in Facebook. My code:
private void SharePhotoFacebook(Bitmap bmp) {
FacebookSdk.sdkInitialize(FaceActivity.this);
// Create a callbackManager to handle the login responses.
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bmp)
.build();
if(ShareDialog.canShow(SharePhotoContent.class)){
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);
}
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.i("David", "Éxito");
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.i("David", "Error");
}
});
}
But no dialog is showing, and nothing is posted in Facebook. I have implemented this code in the same Activity I take photo (FaceActivity), and is the Activity declared in Facebook Developer page of the app. What is wrong?
I can see like something tries to show (a grey "flash" that appears in the screen) but nothing shows.
EDIT: Im taking photo this way:
makePhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
#Override
public void onPictureTaken(#NonNull byte[] bytes) {
Log.i("David", String.valueOf(bytes.length));
bmp= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
ShowShareDialog();
//SharePhotoFacebook(bmp);
}
});
}
});
I just realized I have this in my debug:
Unsupported get request. Object with ID 'XXXXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api [extra]:
I want to share only text via facebook sdk. I researched more time but generally use ShareLinkContent class. Its working but i dont want to add link only post text to user wall via to ShareButton. How i can do?
Code:
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentDescription(Html.fromHtml(shareText).toString())
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Toast.makeText(activity, "Success..", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(activity, "Cancel..", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException e) {}
});
shareButton.setFragment(this);
shareButton.setShareContent(content);
FacebookSDK: 4.11.0
When I try to implement the sharing of links for the app links using the ShareDialog using ShareLinkContent, the image and title are not shown correctly when the actually the post is shared.
String url = "https://fb.me/" + id; //Id is generated by app links hosting api
ShareDialog shareDialog = new ShareDialog(activity);
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent.Builder linkContentBuilder = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(url))
.setContentTitle(contentTitle)
.setContentDescription(contentDescription);
if (imageUrl != null) {
linkContentBuilder.setImageUrl(Uri.parse(imageUrl));
}
ShareLinkContent linkContent = linkContentBuilder.build();
shareDialog.setShouldFailOnDataError(true);
shareDialog.registerCallback(activity.callbackManager,
new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.d(TAG, "Facebook sharing successful");
}
#Override
public void onCancel() {
Log.d(TAG, "Facebook sharing cancelled");
}
#Override
public void onError(FacebookException e) {
Log.d(TAG, "Facebook sharing error");
Log.d(TAG, e.getMessage());
}
});
shareDialog.show(linkContent);
}
But the sharing looks like below:
Facebook sharing
Can someone help me out?
I use facebook sample for story posting:
public static void postStory(Activity activity) {
String fbAppId = activity.getString(R.string.facebook_app_id);
ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
.putString("fb:app_id", fbAppId)
.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();
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
.setActionType("books.reads")
.putObject("book", object)
.build();
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
.setPreviewPropertyName("book")
.setAction(action)
.build();
boolean canShare = new ShareApi(content).canShare();
ShareDialog.show(activity, content);
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
Log.d(TAG, result.toString());
}
#Override
public void onCancel() { }
#Override
public void onError(FacebookException e) {
Log.d(TAG, e.toString());
}
});
}
canShare() return true, ShareDialog work very well, but ShareApi return:
{FacebookGraphResponseException: Invalid parameter httpResponseCode:
500, facebookErrorCode: 100, facebookErrorType: FacebookApiException,
message: Invalid parameter}
What could be the problem?
What SDK version are you using? we had a bug similar to your case that was resolved in v4.1.2+:
Sharing Open Graph objects via the ShareApi could fail to properly
stage nested objects.
I have this simple problem made complicated because of FB. I try to share from android a link and image using facebook sdk.
Did anyone played with ShareOpenGraphObject, ShareOpenGraphAction and ShareOpenGraphContent before, facebook documentation just sucks, no examples at all. I am waiting for examples.
Thanks
let s post some code then:
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();
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
.setActionType("books.reads")
.putObject("book", object)
.putPhoto("image", photo)
.build();
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
.setPreviewPropertyName("book")
.setAction(action)
.build();
shareDialog.show(this, content);
idea is that i don t want to use a book, i just want so share a image a link and a message...how the f i do that? facebook sdk sucks
Its Simple, You can find many examples as well as in Facebook SDK you can find sample for the same...
Bundle postParams = new Bundle();
postParams.putString("link", url);
postParams.putString("picture", imgUrl);
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
I think by using facebook sdk 4.0, you can share contents via share api.
eg:
public void share()
{
ShareLinkContent content=new ShareLinkContent.Builder()
.setContentTitle("Your Title")
.setContentUrl(Uri.parse("website link"))
.setImageUrl(Uri.parse("Image url"))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>(){
#Override
public void onSuccess(Result result){
}
#Override
public void onCancel(){
}
#Override
public void onError(FacebookException error){
}
});
}
For sharing link and image , ShareDialog
Provides functionality to share content via the Facebook Share Dialog and
ShareLinkContent
Describes link content to be shared.
is one method without the Open Graph method.
Snippet is like
private ShareDialog shareDialog;
private boolean canPresentShareDialogWith;
shareDialog = new ShareDialog(this);
canPresentShareDialogWith = ShareDialog.canShow(ShareLinkContent.class);
ShareLinkContent linkContent = new ShareLinkContent.Builder().setContentTitle("Shared from " + "<APP NAME>")
.setContentDescription(
"Question:" + data.getQuestion() + "\n"
// + "Asked by : "
// + data.getName() + "\n"
)
.setContentUrl(
Uri.parse("<Website url>"))
.setImageUrl(Uri.parse(data.getPicUploadPath()))
.build();
if (canPresentShareDialogWith) {
shareDialog.show(linkContent);
} else if (profile != null && hasPublishPermission()) {
ShareApi.share(linkContent, shareCallback);
}
private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
#Override
public void onCancel() {
Log.d("HelloFacebook", "Canceled");
}
#Override
public void onError(FacebookException error) {
Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
String title = getString(R.string.error);
String alertMessage = error.getMessage();
showResult(title, alertMessage);
}
#Override
public void onSuccess(Sharer.Result result) {
Log.d("HelloFacebook", "Success!");
if (result.getPostId() != null) {
String title = getString(R.string.success);
String id = result.getPostId();
String alertMessage = getString(
R.string.successfully_posted_post, id);
showResult(title, alertMessage);
}
}
private void showResult(String title, String alertMessage) {
new AlertDialog.Builder(NewsfeedMain.this).setTitle(title)
.setMessage(alertMessage)
.setPositiveButton(R.string.ok, null).show();
}
};
The method here is sharing a link with the respective web url to load , when clicked from FB feed and the image shared is via a link by the native facebook android app or fallback to sdk share dialog is no facebook app is persent.