简体   繁体   English

Android facebook 登录不适用于已安装的 Facebook 应用程序

[英]Android facebook login not working with installed Facebook app

I have set up simple facebook login.我已经设置了简单的 facebook 登录。 For Android 2.3.6 everything works as should, user gets prompt login dialog, enters data and app goes on.对于 Android 2.3.6 一切正常,用户得到提示登录对话框,输入数据,应用程序继续。 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!我还以为是android版本问题,结果发现手机安装了facebook应用后登录不上!

Tested this on: Galaxy Ace 2.3.6 HTC Desire 4.1.2 Galaxy Note 4.1.2 Android emulator 4.1.2测试于:Galaxy Ace 2.3.6 HTC Desire 4.1.2 Galaxy Note 4.1.2 Android 模拟器 4.1.2

Even the facebook samples are not working!即使是 facebook 样本也不起作用!

Every time the app is executing - else { Log.d("SESSION NOT OPENED", "SESSION NOT OPENED"); }每次应用程序执行时 - else { Log.d("SESSION NOT OPENED", "SESSION NOT OPENED"); } else { Log.d("SESSION NOT OPENED", "SESSION NOT OPENED"); }

It seems like session isn't opened but why is that?好像 session 没有打开,但这是为什么? Followed this guide - https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/遵循本指南 - 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");
            }
        }
    });

i am writting this answer for those who are using Facebook SDK 4.X 我正在为那些使用Facebook SDK 4.X人写这个答案

you can open login portal of facebook in either of the two ways : 你可以用以下两种方式之一打开facebook的登录门户网站:

  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 如果你有Android 1.9.X的Android设备和Facebook App安装在这里称为Native Login Method的设备,你不需要使用facebook WebView

  2. if you have not installed Facebook App in your Android device then it's good to use WebView 如果您尚未在Android设备中安装Facebook App ,那么最好使用WebView

so for this Facebook provide 3 Constants 所以这个Facebook提供3个Constants

  1. NATIVE_ONLY (used when you want to open in Facebook App only) NATIVE_ONLY (仅在Facebook App中打开时使用)
  2. WEB_ONLY (used when you want to open in WebView only) WEB_ONLY (仅在想要在WebView打开时使用)
  3. NATIVE_WITH_FALLBACK (Recommended Facebook detect and opne webView if app is not installed) NATIVE_WITH_FALLBACK (如果未安装应用程序,推荐使用Facebook检测和操作webView

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 请查看以下链接以获取详细信息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);

Get you hash key using this function for both(debug and release apk) and put it in your app in developer.facebook.com/apps 使用此函数获取散列密钥(debug和release apk)并将其放入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.. 这对我很有帮助..希望这对你也有帮助..

Check out the bottom of step 4: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/ 查看步骤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. 如果您没有正确输入您的应用程序密钥哈希,Facebook通过WebView弹出窗口登录(如果未安装该应用程序)仍然有效,但通过本机Facebook应用程序登录将不会。

You should see this exception in LogCat: 您应该在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. Facebook SDK会打印出异常,因此无论如何还要检查是否存在其他问题。

It looks like I couldn't get the data if I had active facebook session (from facebook application). 如果我有活跃的facebook会话(来自facebook应用程序),看起来我无法获取数据。

So before I open the session, I am asking for force login, even If the user has open facebook session from facebook application. 所以在我打开会话之前,我要求强制登录,即使用户已经从facebook应用程序打开了facebook会话。

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. Facebook应用程序打开时,即使是facebook样本也不适合我。

If somebody has better solution, feel free to suggest. 如果有人有更好的解决方案,请随时提出建议。

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 显示本机登录对话框,但在接受权限弹出后,日志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) 但是如果禁用设备上的本机应用程序,则登录和共享将正常工作(在这种情况下,在Web视图中打开登录对话框,并且此处不需要正确的哈希键)

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 散列键与使用openSSl和keytool生成的键不同

and after updating this hash key in Facebook app all works fine 在Facebook应用程序中更新此哈希键后,一切正常

//================================== 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 只需更改您的包名称并获取正确的哈希键即可

There's an important step on the bottom of Facebook's Login Quickstart for Android .Android 的 Facebook 登录快速入门底部有一个重要步骤。

For Android 11 and above, an app needs a queries entry for every app it wants to know about.对于 Android 11 及更高版本,应用需要为它想了解的每个应用创建一个queries条目。 Without this, the app won't know Facebook is installed and will always open the custom Chrome tab.如果没有这个,应用程序将不知道 Facebook 已安装,并且将始终打开自定义 Chrome 选项卡。

Add an entry for com.facebook.katana in your AndroidManifest.xml queries.AndroidManifest.xml查询中添加com.facebook.katana的条目。

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

For reference, I'm using FB SDK 5.6作为参考,我使用的是 FB SDK 5.6

在此输入图像描述

Disable SAndbox Mode...This will allow your app to run on all devices. 禁用SAndbox模式...这将允许您的应用在所有设备上运行。 Try this solution 试试这个解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Facebook 的 Android 应用程序登录不适用于已安装的 Facebook 应用程序 - Android Application Login with Facebook is not working with Facebook App installed Android Facebook登录无法与安装的本机Facebook一起使用 - android facebook login is not working with native facebook installed android:使用facebook sdk 3登录facebook,未安装facebook应用 - android : facebook login using facebook sdk 3 with no facebook app installed 安装了Facebook本机应用程序后,服务器端Facebook登录是否无法在“ cordova android应用程序”上运行? - Server-side Facebook login stops working on a “cordova android app” when facebook native app is installed? Ionic 4:安装本机 facebook 应用程序时,使用 android 登录 facebook 失败 - Ionic 4: failed facebook login with android when the native facebook app is installed Facebook应用程序安装在android中的Facebook登录问题 - Facebook Login issue when facebook app is installed in android 如果在模拟器上安装了Facebook应用,则无法登录到Facebook-Android 4.3 - Login to facebook does not work if facebook app is installed on emulator - Android 4.3 如果安装了本地Facebook应用,则无法使用Facebook集成 - Facebook integration in not working if native facebook app is installed android 当Facebook应用程序已经安装时,Facebook在android中的集成无效 - Facebook integration in android is not working when Facebook app is already installed 安装 Facebook 应用程序时 Facebook 登录崩溃 - Facebook login crashing while Facebook app is installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM