简体   繁体   中英

Android Facebook SDK 4.0 Login without Facebook App

I'm having issues with the webview login for Facebook on Android.

I've followed the tutorials and login works perfectly when the user has the Facebook app installed. When the Facebook app is not installed, the webview for facebook login pops up; however, after logging in and accepting the permissions, the webview simply redirects back to the login screen. It never goes back to my app.

Has anyone else encountered this problem?

    FacebookSdk.sdkInitialize(this);
    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
            if (profile2 != null) {
                loggedIn(profile2);
            } else {
                loggedOut();
            }
        }
    };
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken2) {
            Profile.fetchProfileForCurrentAccessToken();
        }
    };
    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    getProfileInfo();
                }

                @Override
                public void onCancel() {
                    // App code
                    Log.e("Facebook Login", "Login Cancelled");
                    loggedOut();
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Log.e("Facebook Login", "Failed to Login " + exception.toString());
                    loggedOut();
                }
            });

Looking at the logs without filters while the login takes place, I see a couple of possibly relevant logs.

I/chromium﹕ [INFO:CONSOLE(0)] "event.returnValue is deprecated. Please use the standard event.preventDefault() instead.", source:  (0)
I/Auth.Core﹕ [TokenCache] Missing snowballing token: no granted scopes set.

I think you forgot to use callbackManager for proper workflow. Used CallbackManager for registration callback must be called in onActivityResult of host activity. Activity should not be in singleInstance launchMode because it will not be able to launch startActivityForResult(facebook internally launches FacebookActivity using this method). So add to your activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

What was causing the problem was that I was overriding the onclicklistener of the login button to call the login function of the LoginManager. Just don't.

Given your description, I am imagining that you either are not connected on the internet or that you are not handling the login callbacks.

Here is a full facebook 4.0.+ login example .

Edit Based on your code:

You need to use FacebookSdk.sdkInitialize(this); before setContentView() .

This is a working example without using Facebook app and if you close your app and open it again you'll be logged in automatically unless you log out. Here we get the user email address and friends list after log in.

public class MainActivity extends Activity {

    private CallbackManager callbackManager;
    private LoginButton loginButton;
    private static final String TAG = "logTag";
    private TextView mFriend, mEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.activity_main);
        mFriend = (TextView) findViewById(R.id.tv_friend);
        mEmail = (TextView) findViewById(R.id.tv_email);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "Successful Login");
                GraphRequest friendsRequest = GraphRequest.newMyFriendsRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONArrayCallback() {
                            @Override
                            public void onCompleted(JSONArray objects, GraphResponse response) {


                                String x = objects.opt(0).toString();
                                mFriend.setText(x);

                            }
                        });
                GraphRequest meRequest = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                    String email = response.getJSONObject().getString("email").toString();
                                    mEmail.setText(email);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, name, email, gender");
                meRequest.setParameters(parameters);
                meRequest.executeAsync();

                parameters = new Bundle();
                parameters.putString("field", "user_friends");
                friendsRequest.setParameters(parameters);
                friendsRequest.executeAsync();

            }

            @Override
            public void onCancel() {
                Log.d(TAG, "Login Canceled");
            }

            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "Login Error");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        // manage login results
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM