I know that's a question answered before, but no one seems to work and i spend a week on int.
I tried to get my Key Hash from key tool and put them on Facebook app Key hashes, release and debug. No one worked.
"(404) Key hash xxxxxxxxxxxx does not match any storeed key hashes"
That's rare, my keys are different.
So i tried:
PackageInfo info = getPackageManager().getPackageInfo(
"com.pakage.example",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String result = Base64.encodeToString(md.digest(), Base64.DEFAULT);
Toast.makeText(MainActivity.this , result + result.toUpperCase(), Toast.LENGTH_LONG).show();
}
That return me the same Key Has request by Facebook (xxxxxxxxxxxx), so i added them to Facebook App, but Facebook return me "(404) Key hash xxxxxxxxxxxx does not match any storeed key hashes".
When i removed Facebook App from my device, i was avaliebe to share status via WebDialog.
My final code seems to:
if (FacebookDialog.canPresentShareDialog(parent, FacebookDialog.ShareDialogFeature.SHARE_DIALOG))
{
publishFacebookDialog(points); //FacebookDialog
}
else
{
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
publishFeedDialog(points); //WebDialog
}
else
{
Intent intent = new Intent(parent, LoginUsingCustomFragmentActivity.class); //From FacebookSDK demo
parent.startActivityForResult(intent, 666);
}
}
PublishFacebookDialog function:
private void publishFacebookDialog(String puntuacion) {
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(parent)
.setLink("bla")
.setCaption("bla")
.setName("bla")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
}
PublishFeedDialog function:
private void publishFeedDialog(String puntuacion) {
Bundle params = new Bundle();
params.putString("name", "bla");
params.putString("caption", "bla");
params.putString("link", "bla");
WebDialog feedDialog = ( new WebDialog.FeedDialogBuilder(parent,Session.getActiveSession(),params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values, FacebookException error) {
if (error == null) {
final String postId = values.getString("post_id");
if (postId != null) {
} else {
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
} else {
// Generic, ex: network error
}
}
})
.build();
feedDialog.show();
}
The LoginUsingCustomFragmentActivity class is the same that FacebookSDK
Tried:
Remove app from facebook and re add.
Replace symbols like /_+ etc, like from other answered posts.
Use the same key hash reported by "(404) Key hash xxxxxxxxxxxx does not ...." on Facebook App.
Any idea?
Lot of thanks.
I was facing the same problem, the hash code that i generated through openssl was wrong i then used this function and it help. Hope it help you all too.
private void printKeyHash() {
// Add code to print out the key hash
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) {
Log.e("KeyHash:", e.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("KeyHash:", e.toString());
}
}
Use the hash code that is printed in logs and enjoy. Happy Coding :)
See my answer here, there are couple of things to be aware of when adding your hashkey :
https://stackoverflow.com/a/27290250/1495261
Related
I'm working on a project where I need to implement signing in using Facebook authorization on android. I've implemented Facebook API but it provides access token, when I need code (which is used to get token). I found advises saying that I can't use/modify FB api to just get code, instead I have to program my own login flow. I know there is basic documentation on fb developer page but it doesn't say anything about implementing this function on android.
Any help would be much appreciated.
Everything you need is on the official Facebook page.
Manually building a login flow: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
Android SDK: https://developers.facebook.com/docs/android/
Android SDK source code: https://github.com/facebook/facebook-android-sdk
I've found an answer. As I thought, simplest way to do it is by using retrofit. -> Integrate OAuth in Your App.
Code snippet:
// you should either define client id and secret as constants or in string resources
private final String clientId = "xxxxxxxxxxxxxxxxx";
private final String responseType = "code";
/**
* same as in manifest in intent filter
*/
private final String redirectUri = "http://www.example.com/gizmos";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
PackageInfo info = getPackageManager().getPackageInfo(
"xxxxxxxxxxxxxxx", // replace with your unique package name
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
Button loginButton = (Button) findViewById(R.id.loginbutton);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(ServiceGenerator.API_BASE_URL + "/dialog/oauth" +
"?client_id=" + clientId +
"&redirect_uri=" + redirectUri +
"&response_type=" + responseType));
startActivity(intent);
}
});
}
#Override
protected void onResume() {
super.onResume();
// the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(redirectUri)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
if (code != null) {
Log.i("code", code);
// get access token
// we'll do that in a minute
} else if (uri.getQueryParameter("error") != null) {
// show an error message here
}
}
}
}
I'v tried a lot of solutions to implement Facebook login based on the Parse SDK without success.
It seems like I'm missing something.
Generating Facebook hash
I tried to generate the hash with this command (and many others) and password = android:
keytool -exportcert -alias androiddebugkey -keystore C:\Users\Ido\.android\debug.keystore | openssl sha1 -binary | openssl base64
But it didn't work for me, every time I tried to login I got an error that the hash key is not authorized.
Putting this code in the Appliction OnCreate made the work for me (I didn't get the error anymore):
try {
PackageInfo info = getPackageManager().getPackageInfo("com.idob.soccertimer", 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 (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
Application implementation
I made the adjustments for the Facebook and Parse:
Initialize in the onCreate of the Application:
ParseFacebookUtils.initialize(getString(R.string.facebook_app_id));
Overriding the onActivityResult method in my loginFragment:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
}
Add meta tag:
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"/>
I use this code to login:
ParseFacebookUtils.logIn(getActivity(), new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user != null) {
Log.e("MyApp", err.getMessage());
}
if (user == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("MyApp", "User signed up and logged in through Facebook!");
} else {
Log.d("MyApp", "User logged in through Facebook!");
}
}
});
My problem is that the LogInCallback don't call, when I try to login twice - one LogInCallback is returned with user = null and err = null
The OnActivityResult is not called at all.
This path looks wrong to me :
C:\Users\Ido. android\debug.keystore
I'm a linux user and can't be 100% sure on windows paths, but I'm pretty sure it should be located :
C:\Documents and Settings\[User Name]\.android\debug.keystore
or
C:\Users\[User Name]\.android\debug.keystore
The fragment's onActivityResults would not call automatically, it should be called by the activity's onActivityResult
I'm trying to be able to log into Facebook by following the Facebook Getting Started Guide. However a session.isOpened() call always returns false, if the Facebook App is also installed on my phone. If it is not installed on my phone everything work fine. My code is as follows:
public static void loginToFacebook(final Activity activity) {
Log.d("Hello", Global.debug + " Login to Facebook");
Session.openActiveSession(activity, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
Log.d("Session Changed", Global.debug);
if (session.isOpened()) {
Log.d("Session Is Opened", Global.debug);
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
Log.d("Request Completed", Global.debug);
if (user != null) {
Log.d("Hello ", Global.debug + " " + user.getName() + "!");
}
}
});
}
}
});
}
Session Changed is printed to the log, but Session Is Opened never is, until I delete the Facebook app, then it prints the name associated with my Facebook account like it's supposed to after I log in. I've looked around and found that others with the same problem were using the wrong Key Hash, and found the right one by using:
try {
PackageInfo info = getPackageManager().getPackageInfo("com.facebook.login", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Global.debug + " " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
Log.d("Name Not Found", Global.debug);
} catch (NoSuchAlgorithmException e) {
Log.d("No Such Algorithm", Global.debug);
}
But that printed the same Key Hash that I was already using. Is there something that I'm doing wrong? Any help would be greatly appreciated, thank you!
So using the keytool command I was able to generate a hash key for my Android app which uses Facebook login. For purposes of this question, the hash the keytool outputted was "abcdefg=". But when I try to sign on to Facebook from my app, the error says "Key hash abcdefg does not match any stored key hashes" and shows the same exact key I got from keytool just without the equal sign at the end. Why is it not working? Also, when I try to manually type in the key hash on my Facebook developer console (instead of copy/paste), it won't take the key without the equals sign because it only takes keys whose character count is divisible by 4 (my key with equals sign has 28 chars, the key without has only 27 chars). Can someone help?
updateLanguage(getApplicationContext(), "en");
printHashKey(getApplicationContext(),"ur application package name here");
Note1: Create apk file with release keystore.then run on device the keyhash will print logcat. copy the keyhash and place it on facebook app edit setting page.
Note2: correct keyhash generate only on device. dont use simulator for getting keyhash.
public static String printHashKey(Context context, String packagename)
{
String TAG = packagename;
try
{
Log.d(TAG, "keyHash: start");
PackageInfo info = context.getPackageManager().getPackageInfo(TAG,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);
Log.d(TAG, "keyHash: " + keyHash);
return keyHash;
}
Log.d(TAG, "keyHash: end");
}
catch (NameNotFoundException e)
{
Log.d(TAG, "keyHash: name:"+e);
}
catch (NoSuchAlgorithmException e)
{
Log.d(TAG, "keyHash: name:"+e);
}
return "error";
}
public static void updateLanguage(Context context, String code)
{
Locale locale = new Locale(code);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
I am working on android Application. when facebook native application installed it does not work and show only two popup. but if itis not installed it redirect to facebook website and works fine .How i can resolve this . any help will be appreciated. I have checked and added all hashkey in facebook app. but still not working .
statusCallback = new SessionStatusCallback();
Session session = Session.getActiveSession();
this.isLogin = isLoginUser;
session = null;
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(context, null, statusCallback,savedInstanceState);
}
if (session == null) {
session = new Session.Builder(context).setApplicationId(
"688998947797106").build();
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(activity)
.setCallback(statusCallback)
.setLoginBehavior(
SessionLoginBehavior.SSO_WITH_FALLBACK)
.setPermissions(Arrays.asList("email")));
}
}
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(activity)
.setCallback(statusCallback)
.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK)
.setPermissions(Arrays.asList("email")));
} else {
Session.openActiveSession(activity, true, statusCallback);
}
I also Had same problem, I have added different key. See my Question Here
You will get more Information on this discussion Click here See The answer given By Abhishek Agrawal in that Link.
Add following code to your Activities onCreate and check which hashkey is sent by your app to facebook,
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.example.yourpackagename", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String something = new String(Base64.encode(md.digest(), 0));
//String something = new String(Base64.encodeBytes(md.digest()));
Log.e("hash key", something);
}
} catch (NameNotFoundException e1) {
Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
Log.e("exception", e.toString());
}
First Check your hash key with these lines of code given by me at this link Facebook Integration in Android Application and if it is not working yet then please check that your Facebook Android Application in mobile is updated. If the application is not updated update your Facebook application and try to post.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
Use this code in the activity where you call facebook login request.