Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook login, problem with hash key - Flutter , How to generate Facebook Hashkey in flutter?

A window appears with the entrance to Facebook and when you click continue to arise an error

SERVER_ERROR: [code] 1349195 [message]: the key hash does not match any of the saved hashes

login() async {
    final facebookLogin = new FacebookLogin();
    final result = await facebookLogin.logInWithReadPermissions(['email']);
    switch (result.status) {
      case FacebookLoginStatus.loggedIn:
        print(result.accessToken.token);
        Navigator.of(context).pushReplacementNamed('/home_screen');
        break;
      case FacebookLoginStatus.cancelledByUser:
        print('CANCELED BY USER');
        break;
      case FacebookLoginStatus.error:
        print(result.errorMessage);
        break;
    }
  }
like image 617
Max Avatar asked Feb 12 '19 19:02

Max


People also ask

How do I get the key hash login on Facebook?

Go to the Facebook Developer site. Log into Facebook and, using the dropdown menu in the top-right, go to Developer Settings: In your developer settings, select Sample App from the menu, and add and save your key hash into your profile: You can add multiple key hashes if you develop with multiple machines.


2 Answers

  1. Go to your output apk files (Usually <project_root>\build\app\outputs\flutter-apk).

  2. Use keytools to get the SHA1 value (run in terminal / git bash):

    keytool -printcert -jarfile app-debug.apk

  3. Convert the HEX value to base64 to get value that ends with =. (i.e. Use this site. Be aware to have at Input type selected Hex. And delete all : between SHA1 key groups -> AB:CD:EF = wrong, but ABCDEF = correct)

  4. Update that value to key hashes under your android platform.

like image 130
xion Avatar answered Oct 07 '22 22:10

xion


This is how I generated facebook keyhash for android build

  1. Open android project in android studio
  2. Open MainActivity which should be extending to FlutterActivity
  3. Copy this code and paste in MainActivity

import android.content.Context

import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Base64
import android.util.Log
import io.flutter.embedding.android.FlutterActivity
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        printHashKey(this@MainActivity)
    }
    fun printHashKey(pContext: Context) {
        try {
            val info: PackageInfo = pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), PackageManager.GET_SIGNATURES)
            for (signature in info.signatures) {
                val md = MessageDigest.getInstance("SHA")
                md.update(signature.toByteArray())
                val hashKey = String(Base64.encode(md.digest(), 0))
                Log.i("MainActivity", "printHashKey() Hash Key: $hashKey")
            }
        } catch (e: NoSuchAlgorithmException) {
            Log.e("MainActivity", "printHashKey()", e)
        } catch (e: Exception) {
            Log.e("MainActivity", "printHashKey()", e)
        }
    }
}

Alernative Solution :-

Run this command in the terminal to generate debug key

How to generate Facebook hash key

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
like image 26
Quick learner Avatar answered Oct 07 '22 21:10

Quick learner