The supplied auth credential is malformed or has expired. - Flutter Facebook Login

Viewed 3727

I am trying to implement FacebookLogin in my flutter app using Firebase Realtime Database. I am able to login to my app. However, when I try to fetch something from the Database using the FB's auth token, I get the following error:

 E/flutter (30549): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_auth/invalid-credential] The supplied auth credential is malformed or has expired. [ Unsuccessful debug_token response from Facebook: {"error":{"message":"Invalid OAuth access token signature.","type":"OAuthException","code":190,"fbtrace_id":"AwuVQUt6tCG1go"}} ]

Here's my facebook login code:

Future signInFB() async {
      var facebookLogin = FacebookLogin();
    var facebookLoginResult =
        await facebookLogin.logIn(['email']);
     switch (facebookLoginResult.status) {
      case FacebookLoginStatus.error:
        break;
      case FacebookLoginStatus.cancelledByUser:
        print("CancelledByUser");           
        break;
      case FacebookLoginStatus.loggedIn:
        print("LoggedIn");
        var graphResponse = await http.get(
            'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email,picture.height(200)&access_token=${facebookLoginResult
                .accessToken.token}');
          fbToken = facebookLoginResult.accessToken.token.toString();
    var profile = json.decode(graphResponse.body);
    final FirebaseAuth _auth = FirebaseAuth.instance;
    print('Token is ' + fbToken); 
    final OAuthCredential credential =  FacebookAuthProvider.credential(fbToken); 
    final User user = (await _auth.signInWithCredential(credential)).user;
    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);
     _token = await user.getIdToken();
     _userId = user.uid;
    print('Profile is ' + profile.toString());
    break;
  }
  }
3 Answers

I think your key hashes in facebook app is malformed. Facebook app needs Base64 key hashes to work. You have to encode your SHA1 fingerprint from android keystore to Base64.

To do that simply run this line of javascript code in browser console

btoa('your SHA1 key here'.split(':').map(hc => String.fromCharCode(parseInt(hc, 16))).join(''))

Paste the output you receive to key hashes in setting > basic panel of your facebook app on Facebook for Developers

First of all, recheck to make sure that the bundle id/ app id is the same between Facebook and Firebase. Wrong bundle/ app id may cause this issue.

Second, check if the API keys and secret of that you got from facebook is well configured and matching.

Finally, if all is well, in: Facebook for developers -> Settings -> Advanced, please turned off this option "Is App Secret embedded in the client ?"

I had have the seem issue, I searched wile and finally I release that the problem was in the firebase settings : with wrong facebook App id and App secrect so: 1- compare the app id and secret of your facebook app and your firebase project 2- when i was required to give the key hash i used android studio to generate it from my keystore and i got the wrong key, solution : i used this link tomeko to convert my sha1 from my console app (after release and roll out) to hash key and finally this resolved my issue. I hope that helps someone having the same issue.

Related