简体   繁体   English

无法使用Android上的Facebook SDK for Unity登录

[英]Unable to login with Facebook SDK for Unity on Android

The problem 问题

The first time I call FB.Login I'm redirected to facebook but then there seems to be a problem with coming back to my app. 第一次调用FB.Login时,我被重定向到了Facebook,但是回到我的应用程序时似乎出现了问题。 I'm getting the message "[App name] has stopped working." 我收到消息“ [应用名称]已停止工作”。 I've posted a screenshot of the stacktrace further down. 我已经将堆栈跟踪的屏幕截图发布到更下方。

If I after that crash open my game I can see that I am logged in and can do what I want (Invite friends etc.). 如果我在崩溃之后打开游戏,我会看到我已经登录并且可以做我想做的事情(邀请朋友等)。 But if I log out and then in again, the same problem occurs. 但是,如果我注销然后再登录,则会出现相同的问题。

Additional info 附加信息

  • I'm certain that I've got the hash key set up correctly, otherwise I wouldn't be able to call those other fb functions. 我确定我已经正确设置了哈希键,否则我将无法调用其他fb函数。
  • My manifest.xml is using the FBUnityPlayerActivity activity and I'm not extending UnityPlayerActivity in some other activity. 我的manifest.xml使用的是FBUnityPlayerActivity活动,而我没有在其他活动中扩展UnityPlayerActivity。
  • It looks like I'm coming back to my app for a split second before the screen goes black and the crash happens. 似乎我要回到我的应用程序一秒钟,然后屏幕变黑,然后发生崩溃。
  • I'm using Facebook for Unity SDK 4.3.4, although I have tried 4.3.3 and 4.2.4. 我已将Facebook用于Unity SDK 4.3.4,尽管我已尝试4.3.3和4.2.4。 I'm seeing the same error on every version. 我在每个版本上都看到相同的错误。
  • On the app page on facebook, the package name is correct and the Class is com.facebook.unity.FBUnityPlayerActivity. 在facebook上的应用程序页面上,程序包名称正确,并且类为com.facebook.unity.FBUnityPlayerActivity。
  • Out of maybe 30 tries, one time I actually got back to my game after logging in. Without changing any code, next time I tried, it crashed. 在大约30次尝试中,有一次我实际上是在登录后重新进入游戏的。在不更改任何代码的情况下,下次尝试时,它崩溃了。
  • I've tried to remove all of my scenes and only run the facebook example scene. 我尝试删除所有场景,仅运行Facebook示例场景。 Everything works in this case. 在这种情况下,一切正常。 (I later found out that this only works when runnning in portrait mode.) (我后来发现这仅在以纵向模式运行时有效。)
  • Everything works on iOS. 一切都可以在iOS上运行。 This error is just Android. 这个错误只是Android。

Suspicion 怀疑

Of course I googled the stack trace, and it seems there's a similar problem here and here . 当然,我搜索了堆栈跟踪,似乎这里这里都存在类似的问题。 From the SDK for Unity release notes, I noticed that even the latest version of the sdk uses Facebook for Android SDK 3.5.0, which points towards the same issue as the question I linked to. 从Unity SDK发行说明中,我注意到,即使是最新版的sdk,也将Facebook用于Android SDK 3.5.0,这与我链接到的问题相同。 If that is indeed the case, is it possible for me to swap the underlying facebook for android sdk in the facebook for unity sdk and update it to a newer version myself? 如果确实如此,我是否可以在Facebook中将基础facebook换成android sdk,然后在统一sdk中将其自己更新为新版本?

Stacktrace 堆栈跟踪

在此处输入图片说明

I found a solution. 我找到了解决方案。 I don't understand it at all, but it works. 我一点都不了解,但是可以。 Looking at the stacktrace it seems android can't resume the login activity. 查看stacktrace,似乎android无法恢复登录活动。 I tried running the example project and everything worked, as long as I was running in portrait mode . 我尝试运行示例项目,并且一切正常,只要我以纵向模式运行。 When I tilted my phone to landscape mode the same crash happened - even in the example scene. 当我将手机倾斜到横向模式时 ,即使在示例场景中,也发生了相同的崩溃。 Looking at the LoginActivity in the manifest it looked like this: 查看清单中的LoginActivity,如下所示:

<activity android:name="com.facebook.LoginActivity" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation">
</activity>

Notice that the screenOrientation is set to portrait. 请注意,screenOrientation设置为纵向。 It seems that this, in combination with the fact that my games orientation is set to sensorLandscape - android couldn't find the LoginActivity and thus crashing. 似乎这与我的游戏方向设置为sensorLandscape的事实相结合-android找不到LoginActivity并因此崩溃了。 I removed the android:screenOrientation="portrait" and everything works. 我删除了android:screenOrientation =“ portrait”,一切正常。

I am not sure about Unity but basically this problem occurs when the calling activity is not live or destroyed. 我不确定Unity的情况,但是基本上,当调用活动未激活或销毁时,会发生此问题。 In android the method onActivityResult() is called after login. 在android中,登录后将调用onActivityResult()方法。 The Activity is destroyed forcefully by the mobile( Don't keep activities option enables in the developers options settings). 该活动被移动设备强行破坏(开发人员选项设置中的不保留活动选项启用)。 Please Once cross check in the developers options setting that the Don't keep activities is enabled or disabled. 请一次交叉检查开发者选项设置中的“不保留活动”已启用或禁用。

The main problem is the key hash that generate unity. 主要问题是密钥哈希会产生统一性。 The key which shows unity is different you have to put in facebook settings. 显示统一性的键与您在Facebook设置中输入的不同。

Here's the solution! 这是解决方案!

  1. Unzip .apk file and extract META-INF\\CERT.RSA file 解压缩.apk文件并解压缩META-INF \\ CERT.RSA文件
  2. run keytool -printcert -file CERT.RSA 运行keytool -printcert -file CERT.RSA
  3. notice SHA1 bytes are printed like 29:37:F1:CB:06… 注意SHA1字节的打印方式为29:37:F1:CB:06…
  4. copy SHA1 bytes into HEX to BASE64 converter 将SHA1字节复制到HEX到BASE64转换器
  5. see your BASE64 key hash in output field 在输出字段中查看您的BASE64键哈希

That Key BASE64 is the REAL key you have to put in facebook settings on https://apps.facebook.com/nameapp 该密钥BASE64是您必须在https://apps.facebook.com/nameapp上的 Facebook设置中输入的REAL密钥

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM