Search code examples
iosflutterxcodefirebase-authenticationapple-sign-in

Unhandled Exception: [firebase_auth/invalid-credential] Invalid OAuth response from apple.com


Apple Sign In Error

I recently had the problem that Apple Sign In didn't work in my Flutter Application. I put in my Apple ID and Password and it looked like it worked, but in the console I got the following error:

Console Output

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_auth/invalid-credential] Invalid OAuth response from apple.com
#0      FirebaseAuthHostApi.signInWithCredential (package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart:1098:7)
<asynchronous suspension>
#1      MethodChannelFirebaseAuth.signInWithCredential (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:306:22)
<asynchronous suspension>
#2      FirebaseAuth.signInWithCredential (package:firebase_auth/src/firebase_auth.dart:515:9)
<asynchronous suspension>
#3      AuthService.signInWithApple (package:parkbestapp/services/auth_service.dart:73:9)
<asynchronous suspension>

Solution

  • The Solution I found was a single line of code which I added.

    // Request credential for the currently signed in Apple account.
    final appleCredential = await SignInWithApple.getAppleIDCredential(
        scopes: [
            AppleIDAuthorizationScopes.email,
            AppleIDAuthorizationScopes.fullName,
        ],
        nonce: nonce,
    );
    
    // Create an `OAuthCredential` from the credential returned by Apple.
    final oauthCredential = OAuthProvider("apple.com").credential(
        idToken: appleCredential.identityToken,
        rawNonce: rawNonce,
        accessToken: appleCredential.authorizationCode, <-- ADDED THIS LINE
    );
    

    Also update the firebase_auth in the pubspec.yaml

    dependencies:
      firebase_auth: ^5.2.0