Login Approach with Android Facebook 3.0 SDK

浪尽此生 提交于 2020-01-09 10:57:34

问题



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:


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.



来源:https://stackoverflow.com/questions/16948180/login-approach-with-android-facebook-3-0-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!