Login Approach with Android Facebook 3.0 SDK

2019-01-19 09:23发布


While going through the sample program for Facebook Login, I came across SessionLoginSample's LoginUsingActvity.java https://github.com/facebook/facebook-android-sdk/blob/master/samples/SessionLoginSample/src/com/facebook/samples/sessionlogin/LoginUsingActivityActivity.java

and another example has different approach to Login using UiLifecycleHelper as in Scrumptious example https://github.com/facebook/facebook-android-sdk/blob/master/samples/Scrumptious/src/com/facebook/scrumptious/MainActivity.java

What is the difference b/w those two types which one should i fallow for a simple Login with few permissions i am interested .

One more method i came across is using OpenRequest

   OpenRequest op = new Session.OpenRequest((Activity) this);
   op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
   op.setCallback(null);
   List<String> permissions = new ArrayList<String>();
   permissions.add("user_likes");
   permissions.add("email");
   permissions.add("user_birthday");
   op.setPermissions(permissions);

   Session session = new Builder(this).build();
   Session.setActiveSession(session);
   session.openForRead(op);

I am confused a lot to known which could be the best approach ,if there are more than one way to Login.

1条回答
时光不老,我们不散
2楼-- · 2019-01-19 10:02

Every approach works well. The new thing is SDK 3.0 is session management. You just have to manage session state. Apart from your code I will state 3 more and then I'll tell you when to use them.

1.

Using Session object after getting active session.

Session session = Session.getActiveSession();
if(session ==null)
session= new Session(getApplicationContext) // Also use session builder
if (!session.isOpened() && !session.isClosed()) {
    session.openForRead(new Session.OpenRequest(this)
    .setPermissions(Arrays.asList("basic_info"))
    .setCallback(statusCallback));
}
else
{
    Session.openActiveSession(getActivity(), this, true, statusCallback);
}


private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        if(session.isOpened()
          //do something
    } 

2.

By using default LoginButton Widget.

 <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />


LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);       
authButton.setReadPermissions(Arrays.asList( 
                "user_birthday",
                "user_likes",
                "read_stream",
                ));      
Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);

3.

Using Webdialog -

 Bundle bundle = new Bundle();
    bundle.putString("message", "message");
    WebDialog localWebDialog = new WebDialog.Builder(this, "app_id", "oauth", bundle).build();
    localWebDialog.setOnCompleteListener(new WebDialog.OnCompleteListener()
    {
        public void onComplete(Bundle bundle, FacebookException facebookException)
        {

            Session.getActiveSession();
            AccessToken localAccessToken = AccessToken.createFromExistingAccessToken(bundle.getString("access_token"), null, null, AccessTokenSource.WEB_VIEW, null);
            Session.openActiveSessionWithAccessToken(MainActivity.this.getApplicationContext(), localAccessToken, MainActivity.this.callback);
        }
    });
    localWebDialog.show();

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if(session.isOpened()
            //do something
        }

Use 3rd approach in devices 2.3 or lower because previous ones shows unexpected error on most cases.

查看更多
登录 后发表回答