Android Facebook 4.0.1无效密钥哈希错误

时间:2015-04-07 12:06:38

标签: android android-facebook facebook-sdk-4.0

我已经完全按照facebook开发者页面上的说法登录facebook中的facebook。我的代码效果很好。 然而,当用户注销并尝试再次登录时,它会产生无效的密钥哈希错误。我不知道我的代码有什么问题。

的Manifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name="com.example.helltop.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".GameActivity"
            android:screenOrientation="portrait"></activity>
        <activity 
            android:name=".HelpActivity"
            android:screenOrientation="portrait"></activity>
        <activity android:name="com.facebook.FacebookActivity"
          android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
          android:theme="@android:style/Theme.Translucent.NoTitleBar"
          android:label="@string/app_name" />
         <meta-data android:name="com.facebook.sdk.ApplicationId"
                android:value="@string/facebook_app_id" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationName"
            android:value="@string/facebook_app_name" />

        <provider android:authorities="com.facebook.app.FacebookContentProvider137..."
          android:name="com.facebook.FacebookContentProvider"
          android:exported="true"/>

    </application>

登录活动

    CallbackManager callbackManager;
    LoginButton btnFbLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.loginactivity);

        btnFbLogin = (LoginButton) findViewById(R.id.btn_fblogin); btnFbLogin.setOnClickListener(this);btnFbLogin.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        fbLoginGetUserData(loginResult.getAccessToken());
                    }
                    @Override
                    public void onCancel() {
                        Toast.makeText(LoginActivity.this, "Facebook Login Canceled!" , Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onError(FacebookException error) {
                        Toast.makeText(LoginActivity.this, "ERROR : " + error.getMessage() , Toast.LENGTH_SHORT).show();
                    }
                });
        btnFbLogin.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {

            }
        };
    }

private void fbLoginGetUserData(AccessToken accessToken) {

        GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {

            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                if(response.getError() != null){
                    showAlert(response.getError().getErrorCode() + " : " + response.getError().getErrorMessage(), "Facebook Error!");
                } else {
                    // Get the data here
                }
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,first_name,last_name,link,birthday,email,gender,middle_name");
        request.setParameters(parameters);
        request.executeAsync();
    }

这是我如何为facebook创建我的应用程序的发布密钥库和哈希密钥。

keytool -genkey -alias myreleasekey-keyalg RSA -keystore %USERPROFILE%\.android\myapprelease.keystore -keysize 2048 -validity 10000

keytool -exportcert -alias myreleasekey -keystore ~/myapprelease.keystore | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64

1 个答案:

答案 0 :(得分:0)

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        printHashKey();
    }

    public void printHashKey(){

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.parakhidevelopers.happydays",
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("Key Hash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
    }
}