15

I have set up simple facebook login. For Android 2.3.6 everything works as should, user gets prompt login dialog, enters data and app goes on. I thought that it was android versions fault but it turs out that the login isn't working when there is facebook application installed on the phone!

Tested this on: Galaxy Ace 2.3.6 HTC Desire 4.1.2 Galaxy Note 4.1.2 Android emulator 4.1.2

Even the facebook samples are not working!

Every time the app is executing - else { Log.d("SESSION NOT OPENED", "SESSION NOT OPENED"); }

It seems like session isn't opened but why is that? Followed this guide - https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

Code:

Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(final Session session, SessionState state, Exception exception) {

            if (session.isOpened()) {

                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            Log.d("Access_token", session.getAccessToken());
                        }
                    }
                });
            } else {
                Log.d("SESSION NOT OPENED", "SESSION NOT OPENED");
            }
        }
    });
Karlis
  • 1,501
  • 4
  • 19
  • 31
  • Why is `Session session` final? – BLaZuRE Jul 25 '13 at 10:44
  • 2
    So that I could get, for example, user access token inside onComplete method! – Karlis Jul 25 '13 at 11:33
  • Hey have you disabled Sand Box mode in basic info tab of your app on facebook developer's page? – Looking Forward Jul 25 '13 at 11:33
  • Yes. And I I mentioned, if I test on Android 2.3.6 everything works fine and I get my access token, because for Android 2.3 you get and dialog popup where you enter your user credentials, but for Android 4.1 I don't get that dialog popup! – Karlis Jul 25 '13 at 11:37
  • which version of Facebook sdk you are using? – Looking Forward Jul 25 '13 at 11:39
  • I am using facebook-sdk-3.0 – Karlis Jul 25 '13 at 11:40
  • have you set the target in properties when you were adding Facebook sdk as library project? – Looking Forward Jul 25 '13 at 11:43
  • 2
    Yes I have set everything and it works on older devices but not on the 4.1 versions. I just created clean project and went step by step through Facebook getting started tutorial and the same - For 2.3 I get login popup and everything works fine but for 4.1 I am asked if I allow access to certain things and that's it, my data aren't displayed! – Karlis Jul 25 '13 at 13:01
  • What are `state` and `exception` when that code gets triggered? Do you have an active session to open? Put a breakpoint there, examine the full state.. – Delyan Jul 25 '13 at 13:59
  • I found out that there is not problem with Android version! The problem is when I have installed facebook app on my mobile! I don't get promt for log in and can't get data from that app! – Karlis Jul 25 '13 at 14:03
  • Maybe there is some way to force to ask for user login manually without fetching data from facebook application? – Karlis Jul 25 '13 at 14:07

7 Answers7

14

i am writting this answer for those who are using Facebook SDK 4.X

you can open login portal of facebook in either of the two ways :

  1. if you have an android device with Android 1.9.X and Facebook App is Installed in device called Native Login Method here, you don't need to use facebook WebView

  2. if you have not installed Facebook App in your Android device then it's good to use WebView

so for this Facebook provide 3 Constants

  1. NATIVE_ONLY(used when you want to open in Facebook App only)
  2. WEB_ONLY(used when you want to open in WebView only)
  3. NATIVE_WITH_FALLBACK(Recommended Facebook detect and opne webView if app is not installed)

Check below link for detail https://developers.facebook.com/docs/reference/android/current/class/LoginButton/ https://developers.facebook.com/docs/facebook-login/android/v2.2#troubleshooting

     LoginButton.setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK);
     LoginButton.setLoginBehavior(LoginBehavior.NATIVE_ONLY);
     LoginButton.setLoginBehavior(LoginBehavior.WEB_ONLY);
mrid
  • 5,782
  • 5
  • 28
  • 71
Rishabh Agrawal
  • 1,998
  • 2
  • 25
  • 40
7

Check out the bottom of step 4: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/

If you have not entered your app key hash properly, Facebook login via the WebView popup (if the app is not installed) will still work, but login via the native Facebook app won't.

You should see this exception in LogCat:

com.facebook.http.protocol.ApiException: remote_app_id does not match stored id

The Facebook SDK prints its exceptions so check there anyway if there are other problems.

antonyt
  • 21,863
  • 9
  • 71
  • 70
  • Tried this. I get the window to set permisions but nothing shows in LogCat and user isn't logged in – Karlis Jul 26 '13 at 06:59
  • 1
    Actually this was the problem. But Eclipse LogCat didn't show any erros but when I ran adb logcat in terminal it showed the error and it turns out that I have copied has without one character! – Karlis Jul 26 '13 at 08:26
6

Get you hash key using this function for both(debug and release apk) and put it in your app in developer.facebook.com/apps

private void calculateHashKey(String yourPackageName) {
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                yourPackageName,
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:",
                    Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

this help me a lot.. Hope this will help you too..

jignesh.world
  • 1,396
  • 1
  • 11
  • 26
2

It looks like I couldn't get the data if I had active facebook session (from facebook application).

So before I open the session, I am asking for force login, even If the user has open facebook session from facebook application.

openRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);

So now everything works but user has to enter the data manually. It's not ideal but it works.

Even the facebook samples weren't working for me with facebook app opened.

If somebody has better solution, feel free to suggest.

Karlis
  • 1,501
  • 4
  • 19
  • 31
1

If hash key is not properly generated then you can face problems like

A native Login Dialog is displayed but after accepting the permissions popup goes of and nothing happens in log cat

But Login and share will work fine if native app on device is disabled (Login Dialog opens in web view in this case and proper hash key is not required for this)

I was facing the same problem and solved this one by getting hash key using this code. Hash key was different from one generated using openSSl and keytool

and after updating this hash key in Facebook app all works fine

//================================== To Get Facebook Hash key Programmatically =========================//
    PackageInfo info;

     try {
            info = activity.getPackageManager().getPackageInfo("com.checkmyplanner", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md;
                md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                String something = new String(Base64.encode(md.digest(), 0));
                //String something = new String(Base64.encodeBytes(md.digest()));
                Log.e("hash key", something);
            }
        } catch (NameNotFoundException e1) {
            Log.e("name not found", e1.toString());
        } catch (NoSuchAlgorithmException e) {
            Log.e("no such an algorithm", e.toString());
        } catch (Exception e) {
            Log.e("exception", e.toString());
        }

Just change your package name and get proper hash key

HemangNirmal
  • 621
  • 1
  • 8
  • 24
1

There's an important step on the bottom of Facebook's Login Quickstart for Android.

For Android 11 and above, an app needs a queries entry for every app it wants to know about. Without this, the app won't know Facebook is installed and will always open the custom Chrome tab.

Add an entry for com.facebook.katana in your AndroidManifest.xml queries.

<queries>
  <package android:name="com.facebook.katana" />
</queries> 

For reference, I'm using FB SDK 5.6

Aloha
  • 864
  • 18
  • 40
-1

enter image description here

Disable SAndbox Mode...This will allow your app to run on all devices. Try this solution

Looking Forward
  • 3,579
  • 8
  • 45
  • 65