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 )
Related
I want to publish post to user's wall. I know Facebook only allows tester and developers to post on wall. I have already added user to tester list. When I try to get publish permission, it says that user has already granted permission (as shown in screenshot) and returns. I am not able to get permission or post on wall. Moreover, callback's any method is not called as well.
CODE
I have followed code from Facebook Example RPSSample.
//Publish to wall
public void publishResult() {
registerPublishPermissionCallback();
if (canPublish()) { //see definition below
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(urlToPost))
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
callback.didShareOnFacebookSuccessfully();
}
#Override
public void onCancel() {
// This should not happen
}
#Override
public void onError(FacebookException error) {
showToast(error.getMessage());
}
});
}
}
//check if user has permission or not
private boolean canPublish() {
final AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken != null) {
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
// if we already have publish permissions, then go ahead and publish
return true;
} else {
// otherwise we ask the user if they'd like to publish to facebook
new AlertDialog.Builder(activity)
.setTitle(R.string.share_with_friends_title)
.setMessage(urlToPost)
.setPositiveButton(R.string.share_with_friends_yes, canPublishClickListener)
.setNegativeButton(R.string.share_with_friends_no, dontPublishClickListener)
.show();
return false;
}
}
return false;
}
//If user allows, ask Facebook to grant publish_action permission
private DialogInterface.OnClickListener canPublishClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (AccessToken.getCurrentAccessToken() != null) {
// if they choose to publish, then we request for publish permissions
LoginManager.getInstance()
.setDefaultAudience(DefaultAudience.FRIENDS)
.logInWithPublishPermissions(activity,
Arrays.asList(AppConstants.ADDITIONAL_PERMISSIONS));
}
}
};
//Callback - Any of the method doesn't call.
private void registerPublishPermissionCallback() {
LoginManager.getInstance().registerCallback(
callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getPermissions().contains(AppConstants.ADDITIONAL_PERMISSIONS)) {
publishResult();
} else {
handleError("Not enough permissions to publish");
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
handleError(exception.getMessage());
}
private void handleError(String errorMessage) {
// this means the user did not grant us write permissions, so
// we don't do implicit publishes
showToast(errorMessage);
}
}
);
}
My app is live on console. Please guide what is requirement of Facebook to get publish permission with test user? Thanks.
Use below code to check if permission is Granted or not.
String url = "/" + "(userFbID)" + "/permissions";
new GraphRequest(AccessToken.getCurrentAccessToken(), url, null,
HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONObject json = new JSONObject(
response.getRawResponse());
JSONArray data = json.getJSONArray("data");
boolean isPermitted = false;
for (int i = 0; i < data.length(); i++) {
if (data.getJSONObject(i)
.getString("permission")
.equals("publish_actions")) {
isPermitted = true;
String status = data.getJSONObject(i)
.getString("status");
if (status.equals("granted")) {
publishResult()
} else {
LoginFacebook();
}
break;
}
}
if (!isPermitted) {
LoginFacebook();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).executeAsync();
replace user's fb id on place of (userFbID).
If permission is not granted then use this function to ask user for that permission.
private void LoginFacebook() {
loginManager = LoginManager.getInstance();
loginManager.logInWithPublishPermissions(this,
Arrays.asList("publish_actions"));
loginManager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
publishResult()
}
#Override
public void onError(FacebookException error) {
}
#Override
public void onCancel() {
}
});
}
Finally able to solve the problem. There was no issue in code. It was some setting / permission issue with Facebook app. These are steps I followed:
Remove the user from Test role section in your Facebook Developer Console.
Revoke App's Access by logging into that user's account. Go to Settings > Apps. Remove the app from there. You need to wait 10-15 min, since it takes time from Facebook side to revoke access completely.
Make your Facebook App in Sandbox mode (you need to do this to perform publish testing).
Login your android app using any Facebook account other than app admin. It should throw error that app is in development mode.
Add user as Tester in the Facebook Console App. That user may need to approve pending request at his side.
Login from that user account. Facebook will now ask you to provide basic information access to the app. After that you can test publishing. You will be able to post on the user's wall successfully.
Hope this helps someone!
Currently, I am using this tutorial to create the facebook login.
"https://github.com/ParsePlatform/ParseUI-Android"
The Parse user JSON didn't return me an email or gender. I also have research around but I still can't find the solution (well, at least, some other still returning null). Anyone can explain the reason and how to solve it?
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
Arrays.asList(
"public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser","Publish");
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
Arrays.asList( "public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser", "Read");
}
}
});
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
e.toString());
}
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
If we were able to successfully retrieve the Facebook
user's name, let's set it on the fullName field.
*/
ParseUser parseUser = ParseUser.getCurrentUser();
if (fbUser != null && parseUser != null && fbUser.optString("name").length() > 0) {
Log.i("FbUSer", response.getJSONObject().toString());
Log.d("FbUSer-Email",fbUser.optString("email"));
Log.i("FbUSer-gender",fbUser.optString("gender"));
parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
debugLog(getString(
R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
e.toString());
}
loginSuccess();
}
});
}
loginSuccess();
}
}
).executeAsync();
} else {
loginSuccess();
}
}
};
Logcat Section return:
{"id":"1234524124124","name":"FbUser NAme"}
I just found the answer. I just need to set the parameter so I can get the data I want.
GraphRequest request = GraphRequest.newMeRequest(...);
Bundle parameters = new Bundle();
parameters.putString("fields","id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
Reference: https://stackoverflow.com/a/32579366/4961962
for more user info you have to add permissoin to access those detail like email, user_about_me at facebook developer console
select app at facebook developer and go -> app details -> Configure
App Center Permissions
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
I want to implement "Like" and "Comment" feature in my app. I used this code:
public static void like(String postID) {
String grapPath = String.format("%s/likes", postID);
Request request = new Request(Session.getActiveSession(), grapPath,
null, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
Log.i(TAG, response.toString()+" Success!");
}
});
Request.executeBatchAsync(request);
}
public static void postComment(String comment, String postID) {
String grapPath = String.format("%s/comments", postID);
Bundle bundle = new Bundle();
bundle.putString("message", comment);
Request request = new Request(Session.getActiveSession(), grapPath,
bundle, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
Log.i(TAG, "Success!");
}
});
Request.executeBatchAsync(request);
}
Hhow and where can I call these methods to make them work?
Please make sure the prerequisites are set up correctly. Specifically check out the middle of step 4 to make sure you generated your key hash correctly using your debug keystore.
Otherwise code below should help out
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
private void postStatusUpdate() {
if (hasPublishPermission()) {
final String message = "Posting to facebook";
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;
}
}
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