在我的一个应用程序中,我需要从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");
}

当前回答

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

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

其他回答

我也有同样的问题。首先登录,没问题,但接下来,一个无效的键散列。

Unity的Facebook SDK得到了错误的键散列。它从"C:\Users\ your user".android\debug获取键。密钥存储库”,在理想情况下,它应该从您在项目中创建的密钥存储库中获取。这就是为什么它告诉你键哈希没有注册。

正如Madi所建议的,您可以按照此链接上的步骤找到正确的键。只需确保将它们指向项目中的密钥库。否则你就拿不到正确的钥匙。

这可能对有同样问题的人有所帮助。

使用下面的代码生成密钥散列 Keytool -exportcert -alias <your_keystore> alias -keystore <your_keystore_file> | openssl sha1 -binary | openssl base64 如何使用keytool 将其粘贴在Facebook开发人员的必填项域中 在Android Studio中,菜单文件→项目结构 添加签名参数。 选择口味 选择我们创建的签名配置。 选择构建类型 选择构建变体并构建它

我通过在MainApplication.onCreate中添加以下内容来修复这个问题:

try {
    PackageInfo info = getPackageManager().getPackageInfo(
                           "com.genolingo.genolingo",
                           PackageManager.GET_SIGNATURES);

    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String hash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
        KeyHash.addKeyHash(hash);
    }
}
catch (PackageManager.NameNotFoundException e) {
    Log.e("PackageInfoError:", "NameNotFoundException");
}
catch (NoSuchAlgorithmException e) {
    Log.e("PackageInfoError:", "NoSuchAlgorithmException");
}

然后我将其上传到谷歌开发人员控制台,然后下载了派生的APK,无论出于什么原因,它具有完全不同的键散列。

然后我使用LogCat来确定新的密钥散列,并将其添加到Facebook,就像其他用户所概述的那样。

下面的代码将为您提供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.

您必须创建两个键散列,一个用于调试,另一个用于发布。

对于Debug键哈希:

在OS X上运行:

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

在Windows上运行:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
base64

调试键哈希源代码

对于Release键哈希:

在OS X上,运行(将<>之间的值替换为您的值):

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

在Windows上,使用(将<>之间的值替换为您的值):

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

释放键哈希源代码