Android facebook login not working with installed Facebook app

你离开我真会死。 提交于 2019-12-18 02:40:46

问题


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");
            }
        }
    });

回答1:


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.




回答2:


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);



回答3:


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




回答4:


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.




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/17855517/android-facebook-login-not-working-with-installed-facebook-app

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