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.
Related
When we post image on facebook by my android app it's post successfully but when we click on that post on Facebook app (on mobile devices) there is a toast appear "There's a problem opening this app." but when open in WEB and click on that posted image it'll redirect to shared link successfully.
I have use this code to share post on facebook.
GraphRequest request = GraphRequest.newPostRequest(AccessToken.getCurrentAccessToken(),
"me/feed", null, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.i(TAG, response.toString());
//checkPostStatusAndEnableButton();
}
});
Bundle postParams = request.getParameters();
postParams.putString("link",post_url);
postParams.putString("caption", caption);
request.setParameters(postParams);
request.executeAsync();
Is we have use some other action for url for mobile devices?
Try this on :
private static List<String> PERMISSIONS = Arrays.asList("public_profile","user_photos","user_videos", "email","user_likes","user_posts",
"user_hometown", "user_location","user_about_me","user_birthday",
"user_friends","user_relationship_details");
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Log.i(TAG,"onSuccess registerCallback");
}
#Override
public void onCancel() {
// App code
Log.i(TAG,"onCancel registerCallback");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.i(TAG,"onError registerCallback");
}
});
Then call this share link method :
private void shareLink() {
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.build();
ShareDialog shareDialog = new ShareDialog(this);
shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
}
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'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"
i have a big problem for sharing text via ShareDialog. It seems i can only share predefined links,descriptions... but not text. Is there any other way to share text of Facebook from my Android app.
My code is:
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() {
return new FacebookDialog.ShareDialogBuilder(this)
.setCaption("Here Comes the Boom!!!!")
.setName("Hello Facebook")
.setDescription("Here is my description")
.setLink("http://developers.facebook.com/android");
}
It's impossible, you have to use the Facebook api to do this.
More informations here: https://stackoverflow.com/a/22123047/2065418
Code for Facebook sharing text/link
public void post(String post_text) {
Bundle postParam = new Bundle();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// shareDialog.dismiss();
// showPublishResult("Photo Post", response.getGraphObject(),
// response.getError());
}
};
Session session = createSession();
postParam.putString("name", post_text);
postParam.putString("link", "https://developers.facebook.com/android");
Request request = new Request(session, "me/feed", postParam,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}