在我的一个应用程序中,我需要从Facebook获取数据…我是这样做的:

我已经创建了应用ID。它成功登录,但注销后,我登录,然后它给我:

我做错了什么?我正在使用Facebook SDK…我已经在手机上安装了Facebook。它在模拟器中运行良好,但没有安装内置的Facebook应用程序。

这是我的代码:

if (FB_APP_ID == null) {
    Builder alertBuilder = new Builder(this);
    alertBuilder.setTitle("Warning");
    alertBuilder.setMessage("A Facebook Applicaton ID must be " +
                            "specified before running this example: see App.java");
    alertBuilder.create().show();
}

// Initialize the dispatcher
Dispatcher dispatcher = new Dispatcher(this);
dispatcher.addHandler("login", LoginHandler.class);
dispatcher.addHandler("stream", StreamHandler.class);
dispatcher.addHandler("logout", LogoutHandler.class);

// If a session already exists, render the stream page
// immediately. Otherwise, render the login page.
Session session = Session.restore(this);
if (session != null) {
    dispatcher.runHandler("stream");
}
else {
    dispatcher.runHandler("login");
}

当前回答

我看到很多人给出了困难的答案,通过我解决我的问题的答案只是去项目/android文件夹/应用程序使用终端,这是你调试的地方。Keystore文件为

keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64

复制粘贴这个命令,替换你的项目/android/app/build.gradle中的别名和密码

debug {
  storeFile file('debug.keystore')  
  storePassword 'android'
  keyAlias 'androiddebugkey'      <---- alias
  keyPassword 'android'           <---- password
}

其他回答

经过长时间的研究,我们找到了解决办法。

我们将权限设置为:

loginButton.setReadPermissions(public_profile email);

这是第一次工作,但当我们重新登录Facebook时,它给出了无效的哈希错误。

简单的解决方案是将上面的行更改为:

loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));

它的工作就像一种幸福!

Facebook应该返回正确的异常,而不是误导性的无效哈希键错误。

我尝试了之前所有的答案,没有一个对我的客户有帮助!

然后我的客户想起来他的设备上安装了Facebook应用程序。在他把它取下来之后。登录工作正常。

hashkey已经被更改,我已经用错误中的key替换了Facebook开发者控制台中的旧hashkey(如上所述),并且它可以工作!

Facebook应用程序本身可能就是问题所在,所以你最好在安装了Facebook应用程序的设备和未安装Facebook应用程序的设备上解决这一问题,并处理这两种情况。

我是这样解决这个问题的:

首先,您必须获得SHA-1值。有两种方法。

在Android Studio中获取SHA-1值。

点击它 点击签署报告 复制SHA-1的值

OR

从密钥存储文件中获取SHA-1值。

keytool -list -v -keystore keystore_file_name.jks -alias key0

复制SHA-1值到你的剪贴板,像这样:

CD:A1:EA:A3:5C:5C:68:FB:FA:0A:6B:E5:5A:72:64:DD:26:8D:44:84

并打开十六进制-> Base64字符串解码器将您的SHA-1值转换为Base64。

这正是Facebook所需要的。

获取生成的哈希值“********************=”,并将密钥哈希值复制到Facebook应用程序。

根据Facebook登录Android,你必须提供关键哈希值。为了获得它,您将需要用于签署应用程序的密钥。

keytool \
    -exportcert \
    -alias YourKeyAlias \
    -storepass YourStoreKeyPassword \
    -keystore PathToYourKeyStoreFile | openssl sha1 -binary | openssl base64

下面的代码将为您提供Facebook的散列,但您必须遵循以下步骤才能获得发布候选散列。

Copy and paste this code in your main activity try { PackageInfo info = getPackageManager().getPackageInfo( "com.example.packagename", 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) { } catch (NoSuchAlgorithmException e) { } Generate a signed APK file. Connect your phone to a laptop and make sure it stays connected. Install and run the APK file in your phone by manually moving the release APK to your phone. Now look at Android LogCat (use filter KeyHash:). You should see your release hash key for Facebook. Simply copy and paste it in your https://developers.facebook.com/apps. It's under settings. Now you can test the app it should work perfectly well.