14

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.
sihrc
  • 2,728
  • 2
  • 22
  • 43
  • Exactly how are you authenticating? I'm not an Android developer but I'm pretty sure this has to do with your Valid OAuth Redirect URI. See https://developers.facebook.com/apps//settings/advanced/ – Hunter Frazier May 02 '15 at 06:52
  • share your code, for Facebook login –  May 02 '15 at 07:42
  • @HunterFrazier: An invalid redirect URI should result in an error message _before_ the user can accept permissions already. – CBroe May 02 '15 at 11:46
  • I'm pretty certain it has nothing to do with the code, but I'll post it anyway. It's stock login code from the new Facebook SDK. If the user has the facebook app, the login works flawlessly. It's only when the user goes through the web login. It never even enters onActivityResult – sihrc May 02 '15 at 17:11
  • This should work. What device are you testing on? Does this happen on multiple devices? Is there anything in logcat? – Ming Li May 04 '15 at 18:33
  • @MingLi, thanks for your reply. It happens on all the devices I'm testing (Samsung and Nexus). There's nothing from my application in the logcat, but I haven't checked the unfiltered messages. – sihrc May 05 '15 at 15:01
  • I just tried it, and I only see a couple of possible relevant logs (it's really hard to tell). I'll update in my question. – sihrc May 05 '15 at 15:07
  • Do the sample apps (that ship with the SDK) work for you? – Ming Li May 05 '15 at 18:14
  • Same thing is happening to my app. It worked before, not sure what's going on. I'm using the callbackManager and in onActivityResult I have the callbackManager call. – Roberto Sep 11 '15 at 08:22
  • @Lancelot for me, my problem was overriding the button on click and calling Login manually through the manager. If you're still have issues, i wouldn't mind taking a look at it! – sihrc Sep 11 '15 at 18:59
  • @sihrc Thanks for your offer. What I've done is to go back to the live version of our app just to see if it's something I've changed as I'm 100% sure it was working before. And no, it's not working either now. It's like if something has changed on the dialog Facebook opens. The exact same code was working fine. – Roberto Sep 14 '15 at 08:23

4 Answers4

5

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.

sihrc
  • 2,728
  • 2
  • 22
  • 43
5

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);
}
curioushikhov
  • 2,461
  • 2
  • 30
  • 44
3

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().

Community
  • 1
  • 1
Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
1

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);
    }
}
Mohammad Saad
  • 44
  • 1
  • 5