User requests credentials during reauthorization

I am trying to implement the new Facebook SDK 3.0 in my Android application and I am having a problem. The problem that I am facing is that the user is prompted to log in again to grant publication permissions ("post_to_wall"), even if the user is already logged in with read permissions. This only happens if the user does not have the FB application installed. If he has the FB application installed, then he will be asked to grant 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 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 that I added to the onActivityResult activity method that calls the login:

Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);

And this is how I request 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);
    }
}

, , , .

? , ? , , - ?

.

+5
2
+1

facebooks, , , AuthorizationRequest, AuthorizationRequest , , , Session.NewPermissionsRequest , AuthorizationRequest ! AuthorizationRequest . API facebook, , "OnFailed/OnSuccess", , facebook (, , ...) . onFailed/onSuccess, , .

, . , . , , Session.NewPermissionsRequest , , !

, .

  • goto src/com/facebook/Sessions.java
  • 862

    private static Session openActiveSession(Context context, boolean allowLoginUI, OpenRequest openRequest)
    
  • .

Session.NewPermissionsRequest. 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);
        }

, .

@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);
    }
}

.

0

All Articles