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();
}
}
Anyone can help please?
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.
来源:https://stackoverflow.com/questions/50114176/google-sign-in-doesnt-return-any-result-when-use-another-account-to-sign-in