I was using the openForRead to ask for the read permissions and after the callback I set for the openForRead I made another publish stream request using newPermissionsRequest and assigned it with a new callback to it. But the new callback didn't get called.
Does anyone know why is that? It does ask me the publish permission but when i clicked ok, the callback didn't get called.
the callback should add new to the session,
like this
Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(activity, Arrays.asList(FACEBOOK_POST_PERMISSIONS));
session.removeCallback(oldCallback);
session.addCallback(new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) { }
});
session.requestNewPublishPermissions(request);
Please update to the 3.0.1 release. This was a bug that got fixed.
See the changelogs.
Related
I am integrating logging in via Facebook in my app. I have this code that presents the login:
Session.openActiveSession(getActivity(), true, new Session.StatusCallback() {
// callback when session changes state
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.d("s", "SUCCES");
// make request to;2 the /me API
}
if (exception != null) {
Log.d("s", exception.toString());
}
Log.d("s", "state:" + state.toString());
}
});
After I enter my username/password, I get this in the logs:
D/app﹕ com.facebook.FacebookException: Log in attempt aborted.
D/app﹕ state:CLOSED_LOGIN_FAILED
D/app﹕ state:OPENING
What I've tried:
Checked my package name is correct
Checked the hash key is correct (Used the code by facebook)
Taken my app out of sandbox mode.
Checked I have the correct app id in my manifest file
What I am doing wrong?
Thanks!
I think you have this problem because you already have an active session. So just test it before trying to reopen it.
Session.StatusCallback statusCallback = new Session.StatusCallback() {
// callback when session changes state
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.d("s", "SUCCES");
// make request to;2 the /me API
}
if (exception != null) {
Log.d("s", exception.toString());
}
Log.d("s", "state:" + state.toString());
}
Session session = Session.getActiveSession();
if (session!=null && !session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setPermissions(...)
.setCallback(statusCallback));
} else {
Session.openActiveSession(getActivity(), true, statusCallback);
}
Reading through Facebook's SDK code we can extract some conclusions.
You get an exception on your callback with a specific message and state. These two are sent only when the close() Session's method is called, while the state was OPENING, which is at the same time called by setActiveSession, and that one gets called directly by your Session.openActiveSession(...)
The next thing that gets called is session.openForRead(openRequest), which calls open(openRequest, ...), with the current state being CREATED (default state after building a new session). This sets the new session as the active session through setActiveSession(session) which ends up closing the oldSession if there was one active, and here's the thing again, you are getting a FacebookException with a "Log in attempt aborted" on the message, which only gets called if the current state of the previous session was OPENING.
All that means that either you are calling this piece of code twice within a short amount of time or that the authorization process is not going through and ends up on a dead end (most likely).
Before getting deeper on the authorize method I'll double check configuration in facebook (mode, key hashes -different depending on the launching environment: production, degub, etc-, package name and start activity). For simplicity, clear data and caches of your app every time you try (or manually call closeAndClearTokenInformation() from your code.
Let's see what comes out to keep digging into the issue.
Prompt how to correctly use the method openActiveSessionWithAccessToken() in Fasebook API. Authorization pass, get token, and launch a dialogue posting, it appears (I can post). After cleaning token, and a closed session, the method of closeAndClearTokenInformation() I'm trying to open a session already having previously received the token, and the session is opened, there is a dialogue, but alas, writes that an error occurred try again later.
In the dialog box displays:
An error occurred. Please try again later.
and gives an error in the log:
Error Code 110,
Error Description: Invalid user id,
Error Message: Missing user cookie (to validate session user).
Here's the code:
public static void migrateFbTokenToSession() {
AccessToken accessToken = AccessToken.createFromExistingAccessToken(getAccessToken(), new Date(getAccessExpires()), null, AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(Constants.FB_APP_PERMISSIONS));
Session.openActiveSessionWithAccessToken(getContext(), accessToken , new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if(session != null && session.isOpened()) {
Session.setActiveSession(session);
}
}
});
}
You are removed preous session so how will you get access. You need to login again. If you want to maintain session then just use UiLifecycleHelper .
I did it with using createFromString method like this:
AccessToken accessToken = AccessToken.createFromString(savedTokenAsString, facebookPermissions, AccessTokenSource.NONE);
Session.openActiveSessionWithAccessToken(context, accessToken, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) { }
});
But you need to make createFromString method public, because its package-private in facebook sdk.
I have annoying problem with login via facebook with additional share_item permission.
My code to login:
public void startFacebookLogin() {
Session localSession = Session.getActiveSession();
if ((!localSession.isOpened()) && (!localSession.isClosed())) {
Session.OpenRequest localOpenRequest = new Session.OpenRequest(this);
localOpenRequest.setCallback(this);
localOpenRequest.setDefaultAudience(SessionDefaultAudience.EVERYONE);
localOpenRequest.setPermissions("email", "share_item");
localSession.openForRead(localOpenRequest);
return;
}
Session.openActiveSession(this, true, this);
}
And code to obtain session login response:
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
if(state == SessionState.OPENED) {
String facebookToken = session.getAccessToken();
processFacebookToken(facebookToken);
}
}
The problem is when I call startFacebookLoging() first time, state in metod call is "CLOSED_LOGIN_FAILED" and exception is:
com.facebook.FacebookAuthorizationException: UnknownError: ApiException:Cannot provide description for share_item
When startFacebookLoing() is called second time Facebook SDK displaying "ask for permissions dialog", but only ask for "basic_info", after accept state in call() is "OPENED", without exception but permission "email" and "share_item" are not attached to session. I tested it on Facebook SDK 3.6 and 3.8.
Any ideas what is wrong? I was trying found "UnknownError: ApiException:Cannot provide description for share_item" in google but there wasn't any interesting results.
SOLVED:
Ok, the problem is solved. Gracious Facebook developer respond me, that share_item is deprecated and now it doesn't work (Instead you can use publish_stream or something else...). But where wasn't any info in Facebook docs about that, thx for good support FB...
Login and asking permission works just fine. But there is one problem: I need to ask publish permission when user wants to share some date from my app. Here is my code:
ParseFacebookUtils.getSession().requestNewPublishPermissions(new NewPermissionsRequest((Activity) context,
Arrays.asList(Permissions.Extended.PUBLISH_ACTIONS, Permissions.Extended.PUBLISH_STREAM))
.setCallback(new StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (!arePublishPermissionsEnabled(session)) {
inflow.setChecked(false);
facebook.setChecked(false);
twitter.setChecked(false);
}
}
}));
The problem is in handling situation when user canceled request or there is a loss of network connection. In this case I need to do some changes in my UI, but method call is calling only when session state is changed (e.g. granted new permissions) and I can't properly changed my UI. Is anyone faced such problem?
private void requestPublishPermissions(Session session) {
List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");
if (session != null) {
pendingAnnounce = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
newPermissionsRequest.setRequestCode(REAUTH_ACTIVITY_CODE);
Session mSession = Session.openActiveSessionFromCache(this);
mSession.addCallback(callback);
mSession.requestNewPublishPermissions(newPermissionsRequest);
}
}
I am trying to restore user session inside my application. For that I call :
Session.openActiveSessionFromCache(ctx);
Most of the time it works fine. But sometimes I get :
java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
Edit :
I've add this check :
Session activeSession = Session.getActiveSession();
if (activeSession!=null && activeSession.isOpened()){
return activeSession;
}
activeSession = Session.openActiveSessionFromCache(ctx);
and it did not help
Does anybody have a hint how to tackle that problem?
thanks
As the
openActiveSessionFromCache
works like following: "Create a new Session, and if a token cache is available, open the Session and make it active without any user interaction".
So it should be better if you check if the session is open or not before calling openActiveSessionFromCache. you can use
isOpened()
Use this line of code when facebook session is not in opened state.
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (!state.isClosed()) {
Session.openActiveSessionFromCache(ctx);
}
}
};