Если вы хотите, чтобы "UserName, Email Id, URL профиля, Google Id" от Google.
Вы допустили ошибку с "GoogleSignInOptions".Пожалуйста, замените мои GoogleSignInOptions на ваш код GoogleSignInOptions, чтобы решить вашу проблему.
Тогда Ваше решение здесь -> Вам следует следовать приведенному ниже коду.
private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 9001;
private LinearLayout googleBtn;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
googleBtn = view.findViewById(R.id.googleBtn);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(getString(R.string.default_web_client_id))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(Objects.requireNonNull(getActivity()))
.enableAutoManage(getActivity(), 0, connectionResult -> {
Snackbar.make(googleBtn, "Connection failed..", Snackbar.LENGTH_SHORT).show();
Log.e(TAG, "Google connection Error: " + connectionResult.getErrorMessage());
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
//Log.e(TAG,"mGoogleApiClient is connected");
mGoogleApiClient.clearDefaultAccountAndReconnect();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.build();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.googleBtn:
//stopAutoManage first otherwise throws exception Already managing a GoogleApiClient with id 0
if (mGoogleApiClient != null) {
mGoogleApiClient.stopAutoManage(Objects.requireNonNull(getActivity()));
}
loginWithGoogle();
break;
}
public void loginWithGoogle() {
Log.e(TAG, "is connected? " + mGoogleApiClient.isConnected());
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
Objects.requireNonNull(getActivity()).startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
public void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
// Get account information
if (acct != null) {
Name = acct.getDisplayName();
if (acct.getEmail() != null) {
Email = acct.getEmail();
} else {
Email = "";
}
SocialUserId = acct.getId();
Gender = "";
String idToken = acct.getIdToken();
String profileURL = Objects.requireNonNull(acct.getPhotoUrl()).toString();
String status = "Status: \nFullname: " + Name + "\n Email: " + Email + "\nProfile URI: " + profileURL;
Log.i(TAG, "Google signin " + status);
Log.i(TAG, "ID Token: " + idToken);
Log.i(TAG, "ID: " + acct.getId());
//TODO Temporary "acct.getCompId()" pass "idToken"
checkIsUserExists();
}
} else {
hideProgressBar();
Log.e(TAG, "Failed!! Google Result " + result.getStatus());
int status_code = result.getStatus().getStatusCode();
switch (status_code) {
case GoogleSignInStatusCodes.SIGN_IN_CANCELLED:
Snackbar.make(googleBtn, "Google sign in has been cancelled.", Snackbar.LENGTH_SHORT).show();
break;
case GoogleSignInStatusCodes.NETWORK_ERROR:
Snackbar.make(googleBtn, "Application is unable to connect with internet", Snackbar.LENGTH_SHORT).show();
default:
//AppUtils.showSnackBar(LandingActivity.this, btnLogin, GoogleSignInStatusCodes.getStatusCodeString(result.getStatus().getStatusCode()), R.integer.snackbar_duration_3sec);
break;
}
}
}