Share link and text with Android Facebook SDK 3.0 - android

Im trying to upgrade to the Facebook SDK 3.0 and have finally gotten everything to work with Request.newStatusUpdateRequest(). However my app shares/posts text along with a link. I have tried/looked into the following:
Request.newStatusUpdateRequest()
This does not seem to have any options for a Bundle or any other way to include a link and icon.
Request.newRestRequest()
Skipped this because I saw REST was being depreciated.
new WebDialog.FeedDialogBuilder(_activity, session, params).build().show();
This actually works pretty well but the resulting post does not seem to be linked to my Facebook App and I am not sure how this will effect my Facebook insights.
Request.newPostRequest()
From what I have read, this method seems to be the proper way. However, i cannot figure out where to get the GraphObject to pass in as one of the parameters.
What is the PROPPER way to post/share text, link and image to the user's wall? It seems to be Request.newPostRequest() so I will include the code I have for that.
Request request = Request.newPostRequest(session, "me/feed", ??graph_object??, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult("message", response.getGraphObject(), response.getError());
}
});
request.setParameters(params);
Request.executeBatchAsync(request);
But what really is a GraphObject? Where do i get the graph_object? The more I read from FB on GraphObject/OpenGraph/Graph API the more I get confused.
If I am heading down the wrong direction entirely, please tell me. If Request.newPostRequest is the propper way of doing this, please give me more information on the GraphObject param.

Finally managed to get everything I needed with the Facebook SDK 3.0 using the following:
Bundle params = new Bundle();
params.putString("caption", "caption");
params.putString("message", "message");
params.putString("link", "link_url");
params.putString("picture", "picture_url");
Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
if (response.getError() == null) {
// Tell the user success!
}
}
});
request.executeAsync();

I did by using this method.
See if this can help or not.
public static void publishFeedDialog(final Activity current, final String title,
final String caption, final String description, final String link,
final String pictureUrl) {
// start Facebook Login
Session.openActiveSession(current, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
Bundle params = new Bundle();
params.putString("name", title);
params.putString("caption", caption);
params.putString("description", description);
params.putString("link", link);
params.putString("picture", pictureUrl);
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
current, Session.getActiveSession(), params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the
// success
// and the post Id.
final String postId = values
.getString("post_id");
if (postId != null) {
ToastHelper.MakeShortText("Posted");
} else {
// User clicked the Cancel button
ToastHelper
.MakeShortText("Publish cancelled");
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
ToastHelper
.MakeShortText("Publish cancelled");
} else {
// Generic, ex: network error
ToastHelper
.MakeShortText("Error posting story");
}
}
}).build();
feedDialog.show();
}
}
});

To share page or link
Bundle params = new Bundle();
params.putString("link", "link_url");
Request request = new Request(Session.getActiveSession(), "me/feed", params, HttpMethod.POST);
request.setCallback(new Request.Callback() {
#Override
public void onCompleted(Response response) {
if (response.getError() == null) {
// Tell the user success!
}
}
});
request.executeAsync();
For more post parameters see me/feed on developer.facebook.com

Related

Android - How to update Status on Facebook attaching a picture

I'm using the Facebook's SDK for Android. I'm using the newStatusUpdateRequest() method to update my Status with a simple message, it works fine. But I don't find the way to attach a picture or an URL to the post.
private void postStatusUpdate() {
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (user != null && hasPublishPermission()) {
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, place, tags, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
I was reading also this link, with all the facebook API methods, but I can't find the way to post both in my Status: a message and a picture:
https://developers.facebook.com/docs/reference/android/3.0/class/Request/#newStatusUpdateRequest
Has anyone solved already a similar problem?
Update. I think to post a picture I should use the method:
public static Request newGraphPathRequest(Session session, String graphPath, Callback callback)
Where graphPath should be the link to the picture. But I don't know how to join both, message and picture in the same Request.
Thanks you very much for your time
Ok I solved it:
private void postStatusUpdate() {
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (user != null && hasPublishPermission()) {
Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), image, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(getString(R.string.photo_post), response.getGraphObject(), response.getError());
}
});
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Bundle params = request.getParameters();
params.putString("message", message);
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
If anyone has got a better solution, feel free to comment

Getting error on Facebook share using Web dialog on Android " An error occurred, Please try later

I am facing some issue in facebook share using web dialog. Facebook is prompting error message that "An error occurred, Please try later." As onCompleteListener is getting called after clicking on ok on error message dialog. I am not able to find the error code.
Same application is working fine for the Facebook native dialog.
I have also tried same code with different facebook app id and it is working fine. So it doesn't look any code related issue, but related to the application setting on Facebook.
In my app id, I have set app domain, site url and mobile site url. Do we need to send some other parameters as well if we are setting app domain, site url and mobile site url.
Please suggest some solution.
Bundle params = new Bundle();
params.putString("name", mShareText);
params.putString("caption","Build");
params.putString("description", mContext.getString(R.string.social_preconfigured_text));
params.putString("link", mShareUrl);
if(mShareImageUrl!=null){
params.putString("picture", mShareImageUrl);
}
else{
params.putString("picture", " ");
}
// Invoke the dialog
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values, FacebookException error) {
if (error == null) {
Toast.makeText(mContext, mContext.getString(R.string.social_success_text), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(mContext, mContext.getString(R.string.social_failure_text), Toast.LENGTH_SHORT).show();
}
}
}).build();
feedDialog.show();
Thanks,
Manish
Dificult to say without error or code but I use facebook dialog like:
#Override
public void requestPostDialog(Bundle bundle, OnPostingCompleteListener onPostingCompleteListener) {
super.requestPostDialog(bundle, onPostingCompleteListener);
if (FacebookDialog.canPresentShareDialog(mSocialNetworkManager.getActivity(),
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(mSocialNetworkManager.getActivity())
.setLink(bundle.getString(BUNDLE_LINK))
.setDescription(bundle.getString(BUNDLE_MESSAGE))
.setName(bundle.getString(BUNDLE_NAME))
.setApplicationName(bundle.getString(BUNDLE_APP_NAME))
.setCaption(bundle.getString(BUNDLE_CAPTION))
.setPicture(bundle.getString(BUNDLE_PICTURE))
// .setFriends(bundle.getStringArrayList(DIALOG_FRIENDS))
.build();
mUILifecycleHelper.trackPendingDialogCall(shareDialog.present());
} else {
publishFeedDialog(bundle);
}
}
private void publishFeedDialog(Bundle bundle) {
Bundle params = new Bundle();
params.putString("name", bundle.getString(BUNDLE_NAME));
params.putString("caption", bundle.getString(BUNDLE_CAPTION));
params.putString("description", bundle.getString(BUNDLE_MESSAGE));
params.putString("link", bundle.getString(BUNDLE_LINK));
params.putString("picture", bundle.getString(BUNDLE_PICTURE));
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(mSocialNetworkManager.getActivity(),
Session.getActiveSession(),
params))
.setOnCompleteListener(new WebDialog.OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
final String postId = values.getString("post_id");
if (postId != null) {
((OnPostingCompleteListener) mLocalListeners.get(REQUEST_POST_DIALOG)).onPostSuccessfully(getID());
} else {
mLocalListeners.get(REQUEST_POST_DIALOG).onError(getID(),
REQUEST_POST_DIALOG, "Canceled", null);
}
} else {
mLocalListeners.get(REQUEST_POST_DIALOG).onError(getID(),
REQUEST_POST_DIALOG, "Canceled: " + error.toString(), null);
}
mLocalListeners.remove(REQUEST_POST_DIALOG);
}
})
.build();
feedDialog.show();
}

Android - Get profile cover picture from Facebook

I am using Facebook SDK and Parse SDK and I want to retrieve the profile cover picture.
I am doing the following:
new Request(
ParseFacebookUtils.getSession(),
"/me?fields=cover",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.wtf("TAG",
response.toString());
}
}).executeAsync();
But I am not able to get the proper response since it says I need an access token (the user has already been logged in).
{Response:
responseCode: 400,
graphObject: null,
error: {
HttpStatus: 400,
errorCode: 2500,
errorType: OAuthException,
errorMessage: An active access token must be used to query information about the current user.
},
isFromCache:false
}
Is there any fix for this available?
After spending A LOT of hours searching for the answer, I finally got it !!!
The Android SDK for Facebook docs, are too useless.
To solve this problem we just need to set the Graph Path in the second param and a Bundle with fields as third param. Example:
Bundle params = new Bundle();
params.putString("fields", "cover");
new Request(ParseFacebookUtils.getSession(),
"me",
params,
HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
//code...
}
}).executeAsync();
Then we can parse the response object returned in onCompleted as JSON with
response.getGraphObject().getInnerJsonObject();
//or
response.getGraphObject().getProperty("cover");
Source: New Facebook SDK and OAuthException in Graphpath requests thanks to #Jesse Chen
Facebook changed a few things and has some terrible documentation. Hope this helps someone else it's what worked for me.
public void getCoverPhotoFB(final String email, AccessToken accessToken){
if(!AccessToken.getCurrentAccessToken().getPermissions().contains("user_photos")) {
Log.e(L, "getCoverPhotoFB....get user_photo permission");
LoginManager.getInstance().logInWithReadPermissions(
this,
Arrays.asList("user_photos"));
}
////
Bundle params = new Bundle();
params.putBoolean("redirect", false);
params.putString("fields", "cover");
new GraphRequest(
accessToken,
"me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(final GraphResponse response) {
Log.e(L, "getCoverPhotoFB..."+response);
// thread is necessary for network call
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
String picUrlString = (String) response.getJSONObject().getJSONObject("cover").get("source");
Log.d(L,"getCoverPhotoFB.....picURLString....."+picUrlString);
URL img_value = new URL(picUrlString);
Bitmap eventBitmap = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
saveImageToExternalStorage(eventBitmap, email + "_B.png");
homeProfile(profile, email);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
thread.start();
}
}
).executeAsync();
}

Post message on my friends wall with sdk 3.5 android

i want to post message on my friends wall , I got my friend list and their Id now i am using the below code but it not posting any message to my any of friend wall, i'll post my code, in my above code i am using a static id of my friends but it is not posting any message on my friends wall , if i replace the id with my fb id then it is posting on my wall?
private void getfriendlist()
{
Session.openActiveSession(this, true, new Session.StatusCallback()
{ // callback when session changes state #SuppressWarnings("deprecation")
#Override
public void call(final Session session, SessionState state, Exception exception) { if(session.isClosed())
{
Log.i("postStatusUpdate session.isClosed", "message not posted session is closed");
} if (session.isOpened())
{ // Log.i("session.isOpened", "session.isOpened"); //session.requestNewReadPermissions(newPermissionsRequest);
if (!session.isOpened())
{ Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(InvitefriendsActivity.this);
if (openRequest != null) { //openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS); openRequest.setPermissions(Arrays.asList("email,user_birthday,user_location,user_hometown,user_about_me,user_relationships,publish_stream,publish_actions,basic_info,status_update","friends_birthday","read_friendlists")); //openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK); session.openForRead(openRequest);
} }
Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback()
{
#Override public void onCompleted(List<GraphUser> users, Response response) { Log.d("AL",""+response.toString());
for (int i=0;i<users.size();i++)
{ //Log.d("AL",""+users.get(i).toString());
Log.d("AL",""+users.get(i).getId());
Log.d("AL",""+users.get(i).asMap().get("name"));
Bundle params = new Bundle();
params.putString("message", "Hi ");
params.putString("caption", "Hello");
Request request = new Request(session, "100005147887121/feed", params, HttpMethod.POST); RequestAsyncTask task = new RequestAsyncTask(request); task.execute(); } } }); } } }); }
I think this is related to requested Permissions. Are you (you Session) allowed to post to someone else Wall? You will find here a list of available permissions
https://developers.facebook.com/docs/reference/login/extended-permissions/
I think you need to request for 'publish_actions'
Regards
Michael

Facebook post not visible even with public privacy

I'm using Facebook SDK 3 for android to share status in my wall .. I authorize the publication to be visible to all my friend , The post is well published but no one can see it expect me , even with privacy :public
private void postStatusUpdate() {
if (user != null && hasPublishPermission()) {
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
#Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
Change the standbox mode ( desactivate it )

Categories

Resources