[英]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的登录门户网站:
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
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
NATIVE_ONLY
(used when you want to open in Facebook App only) NATIVE_ONLY
(仅在Facebook App中打开时使用) WEB_ONLY
(used when you want to open in WebView
only) WEB_ONLY
(仅在想要在WebView
打开时使用) 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.