4

This seems to be a basic feature but after following the signing-in tutorial, it only works if I choose the account that is already registered on the device.

After choosing 'Use another account' and completing some steps to authenticate it looses the call-back result, (onActivityResult is not called).

I use GoogleSignInOptions: no exception occurs and I can't add any listener to detect exceptions.

However, GoogleApiClient works fine but there are some deprecated methods, thus I decided to switch to GoogleSignInOptions to avoid deprecation issues.
Initizlization:

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

googleSignInClient = GoogleSignIn.getClient(this, googleSignInOptions);

when click:

signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });

private void signIn() {
        googleSignInClient.signOut();
        Intent signInIntent = googleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, 123);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123 && data != null) {
            handleSignInResult(data);
        }
    }

    private void handleSignInResult(Intent data) {
        Task<GoogleSignInAccount> signInTask = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            GoogleSignInAccount googleSignInAccount = signInTask.getResult(ApiException.class);
            if (googleSignInAccount != null) {
                updateUI(googleSignInAccount);
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

enter image description here


Anyone can help please?

Phong Nguyen
  • 6,897
  • 2
  • 26
  • 36

1 Answers1

0

UPDATE: This bug has now been fixed in the Oauth2 Library


So I ran into the same issue as well. What I have found is that this is an open issue reported back in March. So this is probably not an issue with your code.

I found another stackoverflow question about the difference between Oauth2 and googleAuthUtil, using the other library mentioned, I have found that this bug doesn't exist.

I imported this package into my gradle

implementation('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}

and authenticate off of the GoogleAccountCredential object.

    GoogleAccountCredential credential = new GoogleAccountCredential(context, scope);
    startActivityForResult(credential.newChooseAccountIntent(), resultCode);

You need GET_ACCOUNTS permission for this method.

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). You can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](https://stackoverflow.com/help/whats-reputation). - [From Review](/review/low-quality-posts/20144882) – Marek Urbanowicz Jun 28 '18 at 05:18
  • How would you reword this to better answer the question? The issue he is asking about, the `onActivityResult` not being fired is a open bug with google. It's nothing he did wrong. There is another plugin from google for using google authentication that doesn't have the bug, using the other plugin will solve the problem or waiting for them to release a fix. How should I reword this to better answer the question or what information more would you expect to see? – Jorj Xenos Jun 28 '18 at 12:42