Search code examples
androidgoogle-oauthgoogle-signingoogle-account

Android - Google Sign-in getDisplayName() giving me E-Mail instead of Display name on new account add


I am trying to integrate Google Sign-in in my android app.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    PrefUtil.setTaskBarColored(this, R.color.treasure_black);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);
    loginHandler = new LoginHandler(this);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addApi(AppIndex.API).build();
}
@OnClick(R.id.btn_login)
    public void OnLoginButtonClick() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
        DialogClass.showDialog(this, "Signing 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);

    } else {
        DialogClass.dismissDialog(this);
    }
}

private void handleSignInResult(GoogleSignInResult result) {

    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();


        userName = acct.getDisplayName();
        Log.i("userName",userName);
        email = acct.getEmail();
        Uri url=null;
        if(acct.getPhotoUrl()!=null){
            url = acct.getPhotoUrl();

        }
        String imageUrl=null;
        if(url!=null && (!TextUtils.isEmpty(url.toString()))){
            imageUrl=url.toString();
            PrefUtil.putString(Constant.PreferenceKey.USER_IMAGE,imageUrl);

        }
        LoginRequestModel loginRequestModel = new LoginRequestModel(email, userName, imageUrl);
        loginHandler.getUserDetails(loginRequestModel);
        signOut();

    } else {
        DialogClass.dismissDialog(this);
    }
}

Now, when I try to Sign-in into my app, if I use a pre-configured account by selecting it in the AccountChooser dialog, it works perfectly.

However, if in the AccountChooser dialog, I use the "Add account" option of the Account Chooser and add a new account, it gives me E-Mail in userName = acct.getDisplayName() instead of the users Name, and gives me null in url = acct.getPhotoUrl()

But If I logout of my app and use the same account by choosing the SAME account via account chooser, it works perfectly, and I cannot figure out why.

Any help will be appreciated.


Solution

  • I was also getting the same problem, after adding the requestIdToken('Your server client id') in the GoogleSignInOptions now i am getting the all data when we add the new account while google login following is the updated GoogleSignInOptions code, replace the Your server client id with your google projects Outh client id,then you will get proper data.

    GoogleSignInOptions gso = new       GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .requestProfile()
                    .requestIdToken('Your server client id')
                    .build(); 
    

    Also refer the this Google Signin returns display name as null only when Add account in the flow

    Thanks.