Android Question Firebase auth INVALID_APP_CREDENTIAL:App validation failed ]

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hi all ,

I am trying this java code:
B4X:
PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                timeout,                 // Timeout duration
                 TimeUnit.SECONDS,   // Unit of timeout
                ba.activity,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks
But always getting
B4X:
An internal error has occurred. [ INVALID_APP_CREDENTIAL:App validation failed ]

notes :
Google sign in works properly
I get the latest google-services.json each time I change firebase project .
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
If any one wants to try
B4X:
'Activity object
Private auth As FirebaseAuthPhone
This lib is just an edit to Erel's library .
 

Attachments

  • firebaseauthphone.zip
    9.4 KB · Views: 377
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
B4X:
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.FirebaseTooManyRequestsException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.IOnActivityResult;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.ActivityObject;

import anywheresoftware.b4a.BA.ShortName;

import java.util.concurrent.TimeUnit;
// ,"firebase-auth-10.2.6.aar","play-services-auth-10.2.6.aar","play-services-auth-base-10.2.6.aar"
@ShortName("FirebaseAuthPhone")
@DependsOn(values={"com.google.firebase:firebase-auth", "com.google.android.gms:play-services-auth"})
@Events(values={"SignedIn (User As FirebaseUser)", "TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)"})
@Version(0.01f)
// @ActivityObject
public class FirebaseAuthWrapper  implements ConnectionCallbacks, OnConnectionFailedListener {
    @Hide
    public GoogleApiClient googleClient;
    @Hide
    public FirebaseAuth auth;
  
    private IOnActivityResult ion;
    private boolean signOut;
    private String eventName;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    // private BA ba;
    /**
     * Initializes the object. The SignedIn event will be raised if there is already a signed in user.
     */
    public void Initialize(final BA ba, String EventName) {
        // this.ba= ba;
      
      
      
      
      
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(PhoneAuthCredential credential) {
                // This callback will be invoked in two situations:
                // 1 - Instant verification. In some cases the phone number can be instantly
                //     verified without needing to send or enter a verification code.
                // 2 - Auto-retrieval. On some devices Google Play services can automatically
                //     detect the incoming verification SMS and perform verificaiton without
                //     user action.
                // Log.d(TAG, "onVerificationCompleted:" + credential);
                // [START_EXCLUDE silent]
                // mVerificationInProgress = false;
                // [END_EXCLUDE]
                BA.Log("onVerificationCompleted");
                // [START_EXCLUDE silent]
                // Update the UI and attempt sign in with the phone credential
                // updateUI(STATE_VERIFY_SUCCESS, credential);
                // [END_EXCLUDE]
                // signInWithPhoneAuthCredential(credential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                // This callback is invoked in an invalid request for verification is made,
                // for instance if the the phone number format is not valid.
                BA.Log("onVerificationFailed"+e.getMessage());
                // [START_EXCLUDE silent]
             
                // [END_EXCLUDE]

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    // Invalid request
                    // [START_EXCLUDE]
                     BA.Log("FirebaseAuthInvalidCredentialsException");
                    // [END_EXCLUDE]
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    // The SMS quota for the project has been exceeded
                    // [START_EXCLUDE]
                    BA.Log("FirebaseTooManyRequestsException");
                    // [END_EXCLUDE]
                }

                // Show a message and update the UI
                // [START_EXCLUDE]
             
                // [END_EXCLUDE]
            }

            @Override
            public void onCodeSent(String verificationId,
                                   PhoneAuthProvider.ForceResendingToken token) {
                // The SMS verification code has been sent to the provided phone number, we
                // now need to ask the user to enter the code and then construct a credential
                // by combining the code with a verification ID.
        

                // Save verification ID and resending token so we can use them later
             BA.Log("onCodeSent");

                // [START_EXCLUDE]
                // Update UI
              
                // [END_EXCLUDE]
            }
        };
        // [END phone_auth_callbacks]
  
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
        auth = FirebaseAuth.getInstance();
        eventName = EventName.toLowerCase(BA.cul);
        auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null)
                    ba.raiseEventFromDifferentThread(FirebaseAuthWrapper.this, null, 0, eventName + "_signedin", false, new Object[] {AbsObjectWrapper.ConvertToWrapper(new FirebaseUserWrapper(), user)});
            }
        });
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(BA.applicationContext.getResources().getString(GetResourceId("string", "default_web_client_id")))
        .requestEmail()
        .build();
        googleClient = new GoogleApiClient.Builder(ba.context)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
        .build();
        googleClient.connect();
    }
    public void startPhoneNumberVerification(final BA ba,String phoneNumber,Long timeout) {
        final Activity act = ba.sharedProcessBA.activityBA.get().activity;
        // [START start_phone_auth]
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                timeout,                 // Timeout duration
                 TimeUnit.SECONDS,   // Unit of timeout
                act,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks
        // [END start_phone_auth]

        // mVerificationInProgress = true;
    }
  
  
  
  
  
  

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    /**
     * Returns the current signed in user. Returns an uninitialized object if there is no user.
     */
    public FirebaseUserWrapper getCurrentUser() {
        return (FirebaseUserWrapper) AbsObjectWrapper.ConvertToWrapper(new FirebaseUserWrapper(), auth.getCurrentUser());
    }
  
    @Hide
    @Override
    public void onConnected(Bundle arg0) {
        if (signOut) {
            Auth.GoogleSignInApi.signOut(googleClient);
        }
        signOut = false;
      
      
    }
    @Hide
    @Override
    public void onConnectionSuspended(int arg0) {
      
    }
    @Hide
    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        BA.Log("connection failed.");
    }
    /**
     * Sign outs from Firebase and Google.
     */
    public void SignOutFromGoogle() {
        auth.signOut();
        if (googleClient.isConnected()) {
            Auth.GoogleSignInApi.signOut(googleClient);
        }
        else {
            signOut = true;
            googleClient.connect();
        }
    }
    /**
     * Start the sign in process.
     */
    public void SignInWithGoogle(final BA ba) {
        final Activity act = ba.sharedProcessBA.activityBA.get().activity;
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleClient);
        ion = new IOnActivityResult() {

            @Override
            public void ResultArrived(int resultCode, Intent intent) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(intent);
                if (result.isSuccess()) {
                    GoogleSignInAccount account = result.getSignInAccount();
                    firebaseAuthWithGoogle(act, account);
                }
            }

        };

        ba.startActivityForResult(ion, signInIntent);
    }
    /**
     * Retrieves the token id. This token can be sent to your backend server. The server can use it to verify the user.
     *The TokenAvailable event will be raised in the current module.
     */
    public void GetUserTokenId(final BA ba, final FirebaseUserWrapper User, boolean ForceRefresh) {
        User.getObject().getToken(ForceRefresh).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {

            @Override
            public void onSuccess(GetTokenResult arg0) {
                ba.raiseEventFromDifferentThread(FirebaseAuthWrapper.this, null, 0, eventName + "_tokenavailable", false, new Object[] {User, true, arg0.getToken()});
            }
          
        }).addOnFailureListener(new OnFailureListener() {

            @Override
            public void onFailure(Exception arg0) {
                ba.raiseEventFromDifferentThread(FirebaseAuthWrapper.this, null, 0, eventName + "_tokenavailable", false, new Object[] {User, false, ""});
            }
          
        });
      
    }
    private void firebaseAuthWithGoogle(Activity act, GoogleSignInAccount acct) {
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        auth.signInWithCredential(credential)
        .addOnCompleteListener(act, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(Task<AuthResult> task) {
            }

        });
    }
    private int GetResourceId(String Type, String Name) {
        return BA.applicationContext.getResources().getIdentifier(Name, Type, BA.packageName);
    }
  
    @ShortName("FirebaseUser")
    public static class FirebaseUserWrapper extends AbsObjectWrapper<FirebaseUser> {
        public String getEmail() {
            return getObject().getEmail();
        }
        public String getDisplayName() {
            return getObject().getDisplayName();
        }
        public String getUid() {
            return getObject().getUid();
        }
        public String getPhotoUrl() {
            return getObject().getPhotoUrl() == null ? "" : getObject().getPhotoUrl().toString();
        }
      
    }
  

}
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Check this
events :
B4X:
sub eventname_onsuccess (userr as user)


end sub
sub eventname_onfailed(exceptionm as string)

end sub
sub eventname_oncodesent(verificationId as string ,token as Object)

end sub
sub eventname_onverificationfailed(exceptionmsg as string)

end sub
sub eventname_onverificationcompleted

end sub
 

Attachments

  • FireBaseAuthPhone (2).zip
    12.3 KB · Views: 280
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
can you please post full example ,thank you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
any idea how to Getting the code sent to phone SMS
Use FirebaseUI. There is a Phoneauth included; firebase is sending the SMS here. It does not work in all Countries though.

In all where it is not supported you need to use a SMS Provider (from your server) which does send the SMS through the provider.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
can you please post full example ,thank you
I am really sorry it has been a while since I have played with firebase stuff but as DonManfred said , it shouldn't be hard to create your own example , you might also start with other firebase verification libraries samples
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
i already try FirebaseUI but i get this error
B4X:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fbuitest/com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
    at android.app.ActivityThread.access$900(ActivityThread.java:177)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity.onCreate(AuthMethodPickerActivity.java:70)
    at android.app.Activity.performCreate(Activity.java:6283)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    ... 10 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[zip file "/data/app/com.fbuitest-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.view.LayoutInflater.createView(LayoutInflater.java:578)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
    ... 19 more
    Suppressed: java.lang.ClassNotFoundException: android.support.constraint.ConstraintLayout
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 22 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
 
Upvote 0

hanyelmehy

Active Member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…