Unable to login with Sign in with Google in android using CredentialManager

274 Views Asked by At

Unable to login with Sign in with Google in android using CredentialManager.

I am updating the Sign in with Google in my LoginManager.java, and it not opening the Google UI and does nothing for me in android.

My code is here

SDK:

 implementation 'com.google.android.libraries.identity.googleid:googleid:1.1.0'
 implementation 'androidx.credentials:credentials:1.2.0'
 implementation 'androidx.credentials:credentials-play-services-auth:1.2.0'

Code:

  public void login(Context context) {
        CancellationSignal cancellationSignal = null;
        GetPasswordOption getPasswordOption = new GetPasswordOption();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            cancellationSignal = new CancellationSignal();
        }
        GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
                .setFilterByAuthorizedAccounts(true)
                .setServerClientId(web_client_id)
                .build();
        if (Build.VERSION.SDK_INT >= 34) {
            credentialRequest = new GetCredentialRequest.Builder(credentialRequest.getData())
                    /*.setCredentialOptions(List.of(googleIdOption))*/
                    .addCredentialOption(credentialOption)
                    .build();
        }
        if (Build.VERSION.SDK_INT >= 34) {
            credentialManager.getCredential(context,
                    credentialRequest,
                    cancellationSignal,
                    Executors.newSingleThreadExecutor(),
                    new OutcomeReceiver<GetCredentialResponse, GetCredentialException>() {
                        @Override
                        public void onResult(GetCredentialResponse result) {
                            handleSignIn(result);
                        }

                        @Override
                        public void onError(GetCredentialException exception) {
                            // Handle the error
                        }
                    }
            );
        }
    }

   public void handleSignIn(GetCredentialResponse result) {
       android.credentials.Credential credential = result.getCredential();
       credential.getData();
       String username = credential.toString();
       Log.e("usermame", username);
    }

How can I get the accessToken and email for this.

1

There are 1 best solutions below

1
Eslam Faisal On

if it directly login with an account that you logged in before, you should logout first, because it saves the logged in account