On Android, how to deal with facebook expiration in receiver (SDK 3.0)

I am using facebook sdk 3.0.

I would like to use a Facebook session on the receiver. but what can I do if the session has expired?

+3


source to share


4 answers


The SDK is meant to be used in a slightly different way than what you are trying to do here. You don't need to handle object states at all Session

. Check out the Scrumptious tutorial .

This authentication section clearly shows how to handle session state changes.

Basically, facebook provides you with a set of UI lifecycle helper classes that you need to include wherever you need facebook functionality in your application. These helpers will detect if the session is active, expired, etc. And the method is called onSessionStateChanged()

whenever there is a session state change.



All you have to do is perform simple checks in this method and redirect the user accordingly. For example, if the session has expired, you can simply redirect the user to the login page, where there will be a facebook login button (this button is provided by facebook in the SDK and it handles the authentication flow for you). It will ask the user for permissions, authenticate the session, etc. without requiring anything).

Work through the tutorial once and I am sure you will have a much better understanding. Hope this helps.

+2


source


Have you tried the code at https://github.com/facebook/facebook-android-sdk/blob/master/samples/GraphApiSample/src/com/facebook/samples/graphapi/GraphApiSampleActivity.java#L136-L142

private Session createSession() {
    Session activeSession = Session.getActiveSession();
    if (activeSession == null || activeSession.getState().isClosed()) {
        activeSession = new Session.Builder(this).setApplicationId(applicationId).build();
        Session.setActiveSession(activeSession);
    }
    return activeSession;
}

      



This checks if the session is active or not, if not - create a new one.

0


source


I found that the facebook developer site said the following:

Access token points can be generated for Facebook apps. Create access tokens to applications with a different login than other tokens; this can be done in code.

This token type is useful for modifying app settings, creating and managing test users, or reading App Insights data. You can use app tokens to publish or remove content on behalf of the user who has granted permissions for your app. For example, if a user has granted permissions to your publish_stream app, your app can use an access access token to publish a status update on their behalf.

App tokens do not expire unless App Secret is reset. Application access points are unique for each application.

original web page about access token

So, it seems like we don't have to deal with this. If you wish, you can show a message in the notification bar when the token has expired due to some issue.

0


source


In facebook android sdk 3.0, if the session expired you need to reopen the session with the existing token. This can be done from other actions as well (ui thread context).

To reopen an expired session

public void openSession(Context context)
{
    Looper.prepare()
    Session session = new Session(context, APP_ID, null, true);
    session.openWithImportedAccessToken(accessToken, new Date(tokenExpiryTime), new Date(tokenExpiryTime), AccessTokenSource.FACEBOOK_APPLICATION, Arrays.asList(APP_PERMISSIONS), null);
    Looper.loop()
}

      

Read the looper.prepare and loop mechanism before starting the implementation.

0


source







All Articles