I want to post messages on facebook without open dialog using native Facebook android app.
I am using facebook sdk 3.5.2.
I have successfully post messages on wall.
I am facing some problems.
1) if I logout from native Facebook app. Then it post messages to previous user's Facebook wall & it not ask for authentication.
2)If user login with different account in Facebook native app, it still post messages to
previous account( which login first time through app). Not ask for authentication.
Code:-
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
new messagePostAsyn().execute(session);
}
}
});
Method:
private void postStatus(final Session session, String message) {
Bundle _postParameter = new Bundle();
_postParameter.putString("name", message);
_postParameter.putByteArray("picture", bytes);
_postParameter.putString("caption", "To help....");
_postParameter.putString("description", "Testing...");
Request request = new Request(session, "me/photos", _postParameter,
HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
// TODO Auto-generated method stub
System.out.println("response......" + response);
}
});
Request.executeBatchAsync(request);
}
This method used in messagePostAsyn() class.
Try this Code :
public void postToWall(String message) {
Bundle params = new Bundle();
params.putString("message", message);
// Uses the Facebook Graph API
try {
facebook.request("/me/feed", params, "POST");
this.finish();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Related
I'm trying to get the facebook's user's timeline in my Android app.
Here my code :
mLoginButton.setReadPermissions(Arrays.asList("user_about_me", "user_friends", "user_likes",
"user_photos", "user_relationships", "user_posts",
"user_status"));
// If using in a fragment
mLoginButton.setFragment(this);
// Other app specific specialization
// Callback registration
mLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
mAccessToken = loginResult.getAccessToken();
for (String permission : loginResult.getRecentlyGrantedPermissions()) {
Log.d(LOG_TAG, "Granted Permission:" + permission);
}
getUserFeed();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
And after the login, I launch this :
private void getUserFeed() {
Bundle params = new Bundle();
params.putInt("limit", 25);
params.putString("fields", "id,name,link,full_picture,message,story,picture,type,place,from,to");
params.putBoolean("summary", true);
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/home",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
final JSONArray data = response.getJSONObject().getJSONArray("data");
//currentJson = response.getJSONObject();
} catch (JSONException e) {
Log.e("Error: ", e.toString());
}
}
}
).executeAsync();
}
I have this respond code from Facebook :
Requires extended permission: read_stream
I know this permission is depreceted, I'm using the latest API 2.5.
Do you know if we can continue to get the user's timeline now, if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
Thanks :)
Do you know if we can continue to get the user's timeline now,
No, you can’t.
if I replace the "/me/home" by "/me/feed" it's ok, but I just get my posts, not my entire timeline.
/me/home was deprecated together with the permission.
/me/feed is what you can get now, and that’s it.
Which posts you can expect to see is listed here: https://developers.facebook.com/docs/graph-api/reference/v2.5/user/feed#readperms
I used test app id and log on by test user create at dash_board app on facebook develop site, require pulish_actions permission when login using login button widget of facebook sdk but result get postid always = null.
Here is my code:
....
shareDialog = new ShareDialog(MainActivity.this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
if (result.getPostId() != null)
Log.e(TAG, result.getPostId());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
pulishButton.setOnClickListener(this);
try{
loginButton.setPublishPermissions(new String[]{"publish_actions","publish_stream"});
}catch (FacebookException e){
e.printStackTrace();
}
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "success");
loginButton.setEnabled(false);
pulishButton.setEnabled(true);
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out.println("ERROR");
} else {
System.out.println("Success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}
#Override
public void onCancel() {
Log.e(TAG, "On cancel");
}
#Override
public void onError(FacebookException e) {
Log.d(TAG, e.toString());
}
});
The solution is to force ShareDialog to use Feed mode and avoid using FB's App for sharing:
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
I believe this is a bug by FB. They don't send postId to onSuccess callback when using FB App for sharing (postId = null), but they do if you use Feed.
I've heard you can avoid the "postId=null" problem by using Facebook Login and demanding "publish_actions" permission. But I don't think this is the correct way to deal with this problem. Regarding the mentioned permission Facebook states:
Publishing via dialogs or social plugins does not require this permission. Do not request review of this permission if you're only using Share dialog, Feed Dialog, Message Dialog etc, or Social Plugins (e.g. the Like Button.)
Facebook's docs on Share Dialog:
This does not require Facebook Login or any extended permissions, so it is the easiest way to enable sharing on the web.
Facebook confirmed this issue in their latest update:
https://developers.facebook.com/support/bugs/478167629274681/
https://developers.facebook.com/support/bugs/647119912303459/
You can't login directly with "publish_action", you have to first login with any read permission then ask for for publish permissions
https://developers.facebook.com/docs/facebook-login/permissions/v2.4#publishing
i am developing an android app to post a status to user news feed.it works fine with my Facebook developer account. when i uses other(fb) account it show the error message given above. my code is here
currentSession = Session.getActiveSession();//this line is in onCreate Method
after press post button the method open is=>
// Method For Link Sharing
public void updateView(final String msg) {
// TODO Auto-generated method stub
Session.openActiveSession(PostStatus.this, true,
new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
/**
* share the link on facebook
*/
if (session.isOpened()) {
Session.openActiveSessionWithPermissionPublish(
PostStatus.this, true,
new Session.StatusCallback() {
#Override
public void call(Session session,
SessionState state,
Exception exception) {
// /**
/**
* share the video link on facebook
*/
if (session.isOpened()) {
Toast.makeText(
getApplicationContext(),
"Status uploading in progress please wait",
1000).show();
Bundle parameters = new Bundle(
1);
/*
* parameters.putParcelable(
* "picture", bitmap);
*/
parameters.putString("message",
msg);
parameters.putString(
"description",
"Via OneTouchKeys");
Request request = new Request(
Session.getActiveSession(),
"me/feed", parameters,
HttpMethod.POST,
new Request.Callback() {
#Override
public void onCompleted(
Response response) {
showPublishResult(
"!!!!",
response.getGraphObject(),
response.getError());
}
});
request.executeAsync();
}
}
}
/**
* permission to publish on facebook
*/
, Arrays.asList(MY_PERMISSIONS_PUBLISH));
}
}
}
/**
* permission to publish on facebook
*/
);
}
and
// Permissions for access facebook sharing
public static final String[] MY_PERMISSIONS_PUBLISH = {"publish_actions" };
public static final String[] MY_PERMISSIONS_READ = { "basic_info" };
i export the app from eclipse with release key and save it into my developer account. i Don't know whether i don't take permission in a proper way or missing some step to use it for everyone.though it works only for my developer account.
i want to add permission to read email from the facebook android sdk.
code
Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
// TODO Auto-generated method stub
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
et_firstName.setText(user
.getFirstName());
et_lastName.setText(user
.getLastName());
Log.e("data",
user.asMap().get("email")
+ "");
URL image_value;
try {
image_value = new URL(
"http://graph.facebook.com/"
+ user.getId()
+ "/picture");
iv_profileImage
.setImageBitmap(BitmapFactory
.decodeStream(image_value
.openConnection()
.getInputStream()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
}
}
});
}
}
});
what i have tried
I read a lot that this can be done using loginbutton but i don't want to use loginbutton.
also i read that if i don't want to use logingbutton, i have to use openfor session, but i didn't know how to use that
please help me, i have been trying for it for like a week
If you use a LoginButton, use:
loginButton.setReadPermissions(Arrays.asList("email"));
If you start the Session yourself, use:
session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("email")));
requests/permissions helpfull link: https://developers.facebook.com/tools/explorer/
After successfull login, try the request again including the email field.
I want my android application to automatically post a preset message when the user click on a button. The preset message will be set by the user, so I am guessing that is not a violation of Facebook policies. How do I do this?
private static final String[] PERMISSIONS =
new String[] {"publish_stream", "read_stream", "offline_access"};
Facebook authenticatedFacebook = new Facebook(APP_ID);
postButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
authenticatedFacebook.authorize(Tests.this, PERMISSIONS,
new TestPostListener());
}
});
public class TestPostListener implements DialogListener {
public void onComplete(Bundle values) {
try {
Log.d("Tests", "Testing request for 'me'");
String response = authenticatedFacebook.request("me");
JSONObject obj = Util.parseJson(response);
Log.d("Tests", "Testing graph API wall post");
Bundle parameters = new Bundle();
parameters.putString("message", "Amit Siddhpura");
parameters.putString("description", "Hi Mr. Amit Siddhpura");
response = authenticatedFacebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
} catch (Throwable e) {
e.printStackTrace();
}
}
public void onCancel() {
}
public void onError(DialogError e) {
e.printStackTrace();
}
public void onFacebookError(FacebookError e) {
e.printStackTrace();
}
}
You have to create Application on Facebook
And get authenticate from user, then you can get a access_token to post some message through Graph API
I think your application have to request extended permissions : publish_stream, offline_access
There is Facebook-Android-SDK source code on github, you can refer it.
http://developers.facebook.com/docs/guides/mobile
http://www.androidpeople.com/android-facebook-api-example-using-fbrocket/