没有在Logcat中获取facebook密钥哈希

时间:2018-07-17 06:59:49

标签: android

我必须在Android应用程序中使用具有Facebook功能的登录名。为此,我正在使用以下代码来生成facebook密钥哈希。 软件包org.hftech.shop;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        printKeyHash(); 
    }

    private void printKeyHash() {
        try{
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 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 (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用它,它将为您服务

private void PrintHashKey() {

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "Your.package.name",
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.e("There is KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

    }