Token is invalid when connecting to Facebook using Firebase in Flutter

Viewed 256

I am connecting to my Facebook account in Flutter, here is the code:

Future<String> signIn() async {

   final facebookLogin = FacebookLogin();
   final facebookLoginResult = await facebookLogin.logInWithReadPermissions(['email']);
   if (facebookLoginResult.status != FacebookLoginStatus.loggedIn)
     return null;
   final FacebookAccessToken myToken = facebookLoginResult.accessToken;
   if (myToken == null) return null;
   AuthCredential authCredential = FacebookAuthProvider.getCredential(accessToken: myToken.token);
   FirebaseAuth firebaseAuth = FirebaseAuth.instance;
   AuthResult authResult = await firebaseAuth.signInWithCredential(authCredential);
   IdTokenResult idToken = await authResult.user.getIdToken(refresh: true);

   return idToken.token != null ? idToken.token : null;
}

The Facebook connection succeeded and the function returns token. I then try to use this token in my python backend to get user details and I get this response from Firebase:

{'error': {'code': 400, 'message': 'INVALID_ID_TOKEN', 'errors': [{'message': 'INVALID_ID_TOKEN', 'domain': 'global', 'reason': 'invalid'}]}}

When I use a token from createUserWithEmailAndPassword function and tries to read the user details in my python backend I get the correct response from Firebase:

{'kind': 'identitytoolkit#GetAccountInfoResponse', 'users': ...}

here is my backend code:

import pyrebase
def get_user_details(token):
    firebaseConfig = {
      "apiKey" : "...",
      "authDomain" : "...",
      "databaseURL" : "...",
      "projectId" : "...",
      "storageBucket": "...",
      "serviceAccount" : "firebase-adminsdk...",

}

firebase = pyrebase.initialize_app(firebaseConfig)
auth = firebase.auth()
print(auth.get_account_info(token))
1 Answers

the problem was with the pyrebase package

here is the code to retrieve the user details from firebase

def get_user_details(token):
    cred = credentials.Certificate('./google-services.json')
    firebase_admin.initialize_app(cred)
    response = auth.verify_id_token(token)
Related