generate key hash for android to use in facebook - android

when i use this command in windows keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.android\debug.keystore | openssl sha1 -binary | openssl base64 to get android hash this message appear 'keytool' is not recognized as an internal or external command,
operable program or batch file.

Try it programmatically as noted in Getting Started Android SDK:
public void generateFBKeyHash(Context mContext) {
try {
PackageInfo info = mContext.getPackageManager().getPackageInfo(
"YOUR PACKAGE NAME IN YOUR MANIFEST",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.e("fb key hash", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException e) {
Log.e("failed", e.getMessage());
}
}
Note: Ensure you have the meta-data in your manifest
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />

Related

Generating release key hash in Linux OS(Ubuntu) : Android Facebook SDK

How can I generate the right release key hash in Ubuntu?
I have already referred this but i can't get my answer.
After trying many time i got solution for fragment which give me a release key hash.
try {
PackageInfo info = getActivity().getPackageManager().getPackageInfo(
"com.yourappname.app",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash", "KeyHash:" + Base64.encodeToString(md.digest(),
Base64.DEFAULT));
Toast.makeText(getActivity().getApplicationContext(), Base64.encodeToString(md.digest(),
Base64.DEFAULT), Toast.LENGTH_LONG).show();
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
And i got this command from here for terminal which give me release key hash.
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

Invalid Key hash facebook android sdk

I'm trying to use Facebook Android SDK to develop a simple app with the Facebook Login Button. But i'm having trouble with Key Hashes. I've created both a debug key and a release key:(in mac)
Debug key:
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64
i also tryed this code also
public static void showHashKey(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
"com.example.me", PackageManager.GET_SIGNATURES); //Your package name here
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
And then i copied this key hashes in the Facebook Developers page.when i run my app with eclipse then working.but When i export the apk and copy it into the device it does not working.
also i have another problem.if my divice has installed Facebook Application then also not working.
how i can solve my problem?
When you get Invalid key hash, it also shows the correct key hash and says "The keyhash XXXXX didnot match with any of the stored keyhashes" . so copy that displayed keyhash and append "=" at the end as XXXXX= and add it in the key hash section. Hope it works....
this might also work
public static void showHashKey(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
"com.example.me", PackageManager.GET_SIGNATURES); //Your package name here
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}

Generate hash key for android mobile app

I am working on a android app and has to provide the hashkey to facebook. I have tried 3 appoarch but none of them works.
By following the official doc:
1.) I write a command in windows like this
"C:\Program Files\Java\jdk1.7.0_17\bin\keytool.exe" -exportcert -alias androiddebugkey -keystore C:\hk7.keystore | "C:\openssl\bin\openssl.exe" sha1 -binary | "C:\openssl\bin\openssl.exe" base64
output:
57GY9jCxTL9lwhI9oAsjNZN1UJ4=
2.) And the programatically way
try {
PackageInfo info = getPackageManager().getPackageInfo("com.package.mypackage", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT);
Log.e("MY KEY HASH:", sign);
Toast.makeText(getApplicationContext(),sign, Toast.LENGTH_LONG).show();
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
output:
PH//aR6qmcnZxj/n+4oT6AzEMZQ=
Works but only for my device
3.) the alias value
keytool -alias hk7 -exportcert -keystore C:\hk7.keystore | "C:\openssl\bin\openssl.exe" sha1 -binary | "C:\openssl\bin\openssl.exe" base64
output:
kiHnmqD3oxggF1Y7rgTqaWTPZk4=
Only the second way works but it is only for my device . In my app , I log the exception , it said the API key does not match . but it seems each device has its own key. How to fix that? and is it caused my openssl is too old? or the android side implementation problem? Are there any site for testing whether the key is work? Thanks for helping
2nd way is t best one, Now you need to add every device hashkey on which you want to test as your app is not on playstore .. once you upload t app on playstore/sign with a perticular keystore then you have to add only that keyhash.... Use this function to get KEYHASH
private void printKeyHashForThisDevice() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.package", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String keyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
Logger.logger("============================================");
Logger.logger("KeyHash================ ", keyHash);
Logger.logger("============================================");
System.out.println("KeyHash================ " + keyHash);
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}

Error: Invalide android:_key parameter. The key does not match allowed key

Hy! I tried to integrate the facebook sdk with my android app. I obtain my key kashes using this comand:
keytool -exportcert -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64
After that I, i create a new facebook application, where i selected native android app and i give my hashkey.
But when I run the app, I receive this error:
Can someone to help me?
Try this when session open
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.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}

App is misconfigured for Facebook login - not returning the logcat, after setting ENABLE_LOG to true in util.java

Sorry for asking the same question, but I read all the threads posted before and tried everything suggested, but I was still unsuccessful.
I am getting the same:
App is misconfigured for Facebook login.
The issue looks same as here, but I could not get the Logcat printed as an error in red even after setting ENABLE_LOG to true in util.java.
I have checked my app_id and copied the hash key in developer.facebook, and everything looks right. But I don't know where I am going wrong and also that I am getting it right when I use the app without native Facebook app.
But I want to login using native Facebook.
Assuming you're using the latest 3.0 SDK, try the following two options:
Option 1: (Windows)
%KEYTOOLPATH%\keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | %OPENSSLPATH%\openssl sha1 -binary | %OPENSSLPATH%\openssl base64
Example:
C:\Program Files (x86)\Java\jdk1.7.0_09\bin\keytool" -exportcert -alias androiddebugkey -keystore "C:\Home\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64
Use the password: android
Option 2: (Print key hash sent to FB)
(A variation of Facebook SDK for Android - Example app won't work)
Add this code to your activity:
try {
PackageInfo info = getPackageManager().getPackageInfo("your package name, e.g. com.yourcompany.yourapp]", 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) {
}
Example:
In HelloFacebookSampleActivity, make the following temporary modification to the onCreate() method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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) {
}
...
}
Run your sample and you should get logcat output on the KeyHash tag similar to:
12-20 10:47:37.747: D/KeyHash:(936): 478uEnKQV+fMQT8Dy4AKvHkYibo=
Use that value in Facebook's App Dashboard settings for your app.

Categories

Resources