简体   繁体   中英

android facebook login is not working with native facebook installed

I'm trying to implement facebook login in my android app. My app is not asking permissions with native facebook installed in my device. But when i switched to web login interface, the login is working fine. Is it common problem or I'm missing something..?

SplashFragment.java

import java.util.Arrays;

import com.facebook.widget.LoginButton;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SplashFragment extends Fragment {

private final String APP_ID = "XXXXXXXXX";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.splash, container, false);
    LoginButton authButton = (LoginButton) view
            .findViewById(R.id.login_button);
    //authButton.setFragment(this);
    authButton.setApplicationId(APP_ID);
    authButton.setReadPermissions(Arrays
            .asList("read_stream", "basic_info","public_profile"));

    return view;
}
}

I'm using LoginActivity.java provided by facebook sdk.

When ever we try to generate hashkey with keytool(through command prompt) i am facing this issue.by generating hash key with code we can resolve this issue. sample code:

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

// Add code to print out the key hash

    try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.facebook.samples.hellofacebook", 
            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) {

}

... Reference link: please check this link

To make facebook login working with native application you need to generate hashkey like this

For Linux

Open Terminal :

For Debug Build

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

you wil find debug.keystore from ".android" folder copy it from and paste on desktop and run above command

For release Build

keytool -exportcert -alias <aliasName> -keystore <keystoreFilePath> | openssl sha1 -binary | openssl base64

NOTE : Make sure In Both case it must ask for password. If it does not asks for password that means something is wrong in command.

Where To set HashKey?

1) Open facebook developer page

https://developers.facebook.com/apps/

2) Follow the screenshot

在此处输入图片说明

Use that method to generate the hash key .. the command line way always gave me wrong hashes

public static String getAppKeyHash(Context context) {
    // Add code to print out the key hash
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
                "com.example.app", PackageManager.GET_SIGNATURES);
        String hash = null;
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            hash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.d("KeyHash:", hash);
        }
        return hash;
    } catch (NameNotFoundException e) {
        return null;
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

replace com.example.app with your package name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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