I used google tutorial https://developers.google.com/identity/sign-in/android/start-integrating. But when I click sign-in button app crashes with message:
com.google.android.gms.common.api.ApiException: 8:
I searched for this error and find: https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes.html#INTERNAL_ERROR, but as you see there is no explanation why this error occurs.
Here is Java code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int RC_SIGN_IN = 9001;
private GoogleSignInClient mGoogleSignInClient;
private SignInButton signInButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
signInButton = findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sign_in_button:
signIn();
break;
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
signInIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
//handleSignInResult(task);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.i("User",account.toString());
Log.i("Mail",account.getEmail());
Log.i("Name",account.getDisplayName());
} catch (ApiException e) {
e.printStackTrace();
Log.i("Error",e.getMessage());
}
}
else {
Log.i("User","invalid");
}
}
Here is layout of the activity above:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>