Android Facebook SDK 3 - request new permission but can't reauthorized - android

I have some problem with my code, i've followed hellofacebook example to post photo. But when i try to make new permission "publish_action", the code doesn't callback anything. Here is my code
private void performPublish(PendingAction action, boolean allowNoSession){
Session session = Session.getActiveSession();
if(session != null){
Toast.makeText(getApplicationContext(),"enter session unnull",Toast.LENGTH_SHORT).show();
pendingAction = action;
if(hasPublishPermission()){
Toast.makeText(getApplicationContext(),"enter haspublish",Toast.LENGTH_SHORT).show();
handlePendingAction();
Toast.makeText(getApplicationContext(),"Posted Success",Toast.LENGTH_SHORT).show();
return;
} else if(session.isOpened()) {
Toast.makeText(getApplicationContext(),"enter session opened",Toast.LENGTH_SHORT).show();
Session.NewPermissionsRequest newPermission = new Session.NewPermissionsRequest(Third.this,PERMISSION);
session.requestNewPublishPermissions(newPermission);
Toast.makeText(getApplicationContext(),"Posted Failed",Toast.LENGTH_SHORT).show();
return;
}
if(allowNoSession) {
pendingAction = action;
handlePendingAction();
}
}
}
Please help me if you can, because i'm newbie with facebook SDK. Thank you

according to new facebook sdk, you need to submit your app for review if you are publishing anything. after facebook approval you can upload picture by using any login. you can check this link for review process
https://developers.facebook.com/docs/apps/review/
https://github.com/sauce/guide/wiki/Facebook's-approval-process

Related

WebDialog share post on Facebook when user removes app from settings

Hi I have problem with Facebook:
Case:
1.User has no Facebook app.
2.User logins into Facebook via WebDialog
3.User gives all permissions for share, and shares post
4.User enters Facebook account, than into applications, and removes my app.
5.User tries to make share again.
6."Unknown error. Please try again later" Appears in WebDialog.
Is there a way to fix this case?
I found that using ShareDialog i can avoid this problem when user has facebook app installed, but I don't know how to solve it if user has no fb app on his phone.
To show dialog I verify:
private boolean checkFacebookLogin(){
Session session = Session.getActiveSession();
if(session!=null && session.isOpened() ){
return true;
}
return false;
}
Than i ask for permissions if they are needed:
private void performPublish() {
Session session = Session.getActiveSession();
pendingAction = PendingAction.POST_STATUS_UPDATE;
if (session != null && mCurrentActivity!=null) {
if (hasPublishPermission()) {
// We can do the action right away.
handlePendingAction();
} else {
// We need to get new permissions, then complete the action when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(mCurrentActivity, PERMISSIONS));
}
}
}
In the end i show WebDialog:
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(mCurrentActivity,
Session.getActiveSession(),
postParams))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,
FacebookException error) {
}
})
.build();
feedDialog.show();
After showing WebDialog, it redirects to error page with "Unknow error [...]" text, i have found no error information, so I don't even know that something goes wrong.
I tried HelloFacebookSample, but there if user has no facebook app, he can't edit post in facebook dialog. I want to see Facebook dialog in both cases ( with/without fb app installed).
if (FacebookDialog.canPresentShareDialog(this,
FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(
this)
.setLink(// what ever you want to share use here
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
Log.d("Tag", "Success!");
publishFeedDialog();
} else {
//ask the user to login .
//authButton.performClick();
share = true;
// }
}
So from the above code if the fb app is already installed it will open that app else you have to ask the user to login by performing Fb LoginButton . performClick(). so the user will be redirected to web dialog of fb login. the onLogin success call back u can share using.,
private void publishFeedDialog() {
Bundle params = new Bundle();
params.putString("link",
"");
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(
MenuActivity.this, 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) {
Toast.makeText(MenuActivity.this, "Posted",
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(
MenuActivity.this
.getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(
MenuActivity.this.getApplicationContext(),
"Publish cancelled", Toast.LENGTH_SHORT)
.show();
} else {
// Generic, ex: network error
Toast.makeText(
MenuActivity.this.getApplicationContext(),
"Error posting story", Toast.LENGTH_SHORT)
.show();
}
}
}).build();
feedDialog.show();
}

Detect whether a user has removed my app from his facebook apps

How can I check if a user has removed my app from his facebook apps before calling a dialog request.
Session session = Session.getActiveSession();
if (session != null) {
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
// //Log.d(TAG,
// "the session doesnt have the permissions");
Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(this);
openRequest.setPermissions(PERMISSIONS);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.openForPublish(openRequest);
return;
} else {
// //Log.d(TAG,
// "the session has enough permissions");
displayFacebookDialog();
}
}
The else is still being called even that the user has deleted the app.
public static WebDialog displayFacebookDialog(String picture, OnCompleteListener callback,
Activity activity) {
Bundle data = new Bundle();
data.putString("name", activity.getString(R.string.app_name));
data.putString("caption", activity.getString(R.string.facebook_caption));
data.putString("description",
activity.getString(R.string.facebook_description));
data.putString("link", activity.getString(R.string.facebook_link));
data.putString("picture", picture);
WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(activity,
Session.getActiveSession(), data)).setOnCompleteListener(callback).build();
return feedDialog;
}
Alright, so it looks like the permissions are bound to the Session object at creation and no request to confirm them is made when you call getPermissions. What this means is that you can have the out of sync state you're describing where your access token is no longer valid, but your application doesn't know that.
I assume what is happening is that you're getting a FacebookRequestError in your postStatusUpdate() method. You should check for this error in your callback and then put the login logic into that method.

Facebook Android SDK Session openForPublish not creating a new session

In the Facebook Android SDK when I call
Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForRead(new OpenRequest(this).setPermissions(FB_PERMISSIONS));
It gets a FB session and every thing runs as normal. But when I replace Read with Publish. i.e. follows
Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForPublish(new OpenRequest(this).setPermissions(FB_PERMISSIONS));
It gives an error saying, that the session is empty, and cannot get publish permissions to empty session.
Can you please tell why is it like this and what would be the best way to handle this?
It took me a while to sort this out so a user could click a button to share one of my products on Facebook in a feed. I didn't want them to be prompted to sign in until they actually wanted to share, so I really just wanted publish permission. The following stacks the initial login/read permission request with the publish permission request. This will double-prompt the users, first for read, then for publish, but that is required now regardless of the solution:
Session session = Session.getActiveSession();
if (session == null) {
session = new Session.Builder(this).setApplicationId("<APP ID HERE>").build();
Session.setActiveSession(session);
session.addCallback(new StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (state == SessionState.OPENED) {
Session.OpenRequest openRequest = new Session.OpenRequest(FacebookActivity.this);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.requestNewPublishPermissions(
new Session.NewPermissionsRequest(FacebookActivity.this, PERMISSIONS));
}
else if (state == SessionState.OPENED_TOKEN_UPDATED) {
publishSomething();
}
else if (state == SessionState.CLOSED_LOGIN_FAILED) {
session.closeAndClearTokenInformation();
// Possibly finish the activity
}
else if (state == SessionState.CLOSED) {
session.close();
// Possibly finish the activity
}
}});
}
if (!session.isOpened()) {
Session.OpenRequest openRequest = new Session.OpenRequest(this);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.openForRead(openRequest);
}
else
publishSomething();
The short answer is, don't call openForPublish. Call openForRead, and then requestNewPublishPermissions later if you need publish permissions.
The long answer is, you can't request publish permissions (on a user who's never connected with Facebook before via your app) unless you already have basic or default permissions already (what you would get if you call openForRead with an empty permission set). So openForPublish actually handles a very specific niche use case that most apps probably don't have.

Logout in Facebook Using android fb SDK 3.0

my app using android facebook sdk 3.0,after login getting some response,after that i need to signout from facebook clicking on button.
How to log-out from the session in facebook
pls help
You should do something like this.
if (Session.getActiveSession() != null) {
Session.getActiveSession().closeAndClearTokenInformation();
}
Session.setActiveSession(null);
Also if you store the user token in some other way you should clear that too.
This method will help you to logout from facebook programmatically in android
/**
* Logout From Facebook
*/
public static void callFacebookLogout(Context context) {
Session session = Session.getActiveSession();
if (session != null) {
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
} else {
session = new Session(context);
Session.setActiveSession(session);
session.closeAndClearTokenInformation();
//clear your preferences if saved
}
}

User is asked for login credentials during reauthorization

I am trying to implement the new Facebook SDK 3.0 into my Android app and I have run into a problem. The problem I am having is that the user is asked to log in again to give publish ("post_to_wall") permissions, even though the user is already logged in, with read permissions. This only happens if the user doesn't have the FB application installed. If he has the FB application installed, then he is only asked to grant the permissions.
This is how I implemented the login:
public void login(Activity activity) {
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
openActiveSession(activity, true, sessionStatusCallback);
}
}
private Session openActiveSession(final Activity activity, final boolean allowLoginUI, final StatusCallback callback) {
return openActiveSession(activity, allowLoginUI, new OpenRequest(activity).setCallback(callback));
}
private Session openActiveSession(final Context context, final boolean allowLoginUI, final OpenRequest openRequest) {
Session session = new Builder(context).setApplicationId(FACEBOOK_APPLICATION_ID).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
This is the callback's call method:
public void call(final Session session, final SessionState state, final Exception exception) {
if (session.isOpened()) {
if (state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
// code if new permissions have been granted
} else {
// code for login
}
} else if (session.isClosed()) {
// code for user canceled login
} else if (exception != null) {
// code if there were errors during login
}
}
}
This is the code I added to onActivityResult method of the activity that calls the login:
Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);
And this is how I ask for new permissions:
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
if (DONT_HAVE_PERMISSIONS) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(activity,
FACEBOOK_PERMISSIONS).setRequestCode(FACEBOOK_AUTHORIZE_ACTIVITY_CODE);
session.requestNewPublishPermissions(newPermissionsRequest);
}
}
I've tried to find out more about this problem, and I only found out some hints that this is intended, but I haven't found anything concrete.
Is this the default behavior? If so, is there a way around it? Or, perhaps I did something wrong?
Thanks for the help.
Update your SDK version; this issue is resolved in Facebook Android SDK v3.0.1.
Looking at facebooks source code I think it should be possible to start trying to get the permissions directly as both login, and permissions classes derive from the same AuthorizationRequest class, and the AuthorizationRequest class does all the work, like really all the work. The Session.NewPermissionsRequest class just makes some private methods, public in the AuthorizationRequest class and that's it! They might as well give us access to AuthorizationRequest directly. The new facebook API doesn't seem to have any form of "OnFailed/OnSuccess" callbacks, so I end up having a state machine to remember the goal of firing up facebook (login, permissions, get friends list ...), and which step I'm on. If they have done some form of onFailed/onSuccess callbacks it would be simple to make a chain rather than keeping track of a state machine.
I haven't tried what I said. If I do, I'll update the answer. If you try and it works to just fire up Session.NewPermissionsRequest directly without logging in let me know!
Update
I got it working with only asking for credentials once as I explained above.
goto src/com/facebook/Sessions.java
On line 862 you will find
private static Session openActiveSession(Context context, boolean allowLoginUI, OpenRequest openRequest)
make it be a public function and save.
Now instead of creating the Session.NewPermissionsRequest object. Make Session.OpenRequest
permisions = new ArrayList<String>();
permisions.add("user_photos");
permisions.add("friends_photos");
Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(
activity, permisions);
request.setCallback(helper);
if (Session.getActiveSession() != null)
Session.getActiveSession().requestNewReadPermissions(request);
else {
Session.OpenRequest orequest = new Session.OpenRequest(activity);
orequest.setPermissions(permisions);
orequest.setCallback(helper);
// its now public so you can call it
Session.openActiveSession(act, true, request);
}
Now make sure you do set a callback, for one important reason
#Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isClosed()) {
// we are doing unofficial stuff so we loose guarantees.
// Set the active session to null if we logout or user cancels
// logging in. If you don't do this, the second time it will result
// in a crash.
Session.setActiveSession(null);
}
}
Now it will ask for all permissions directly and login in one go.

Categories

Resources