2016-11-29 17 views
3

私はアンドロイドアプリにGoogle Sign-inを統合しようとしています。Android - GoogleサインインgetDisplayName()は新しいアカウントに表示名の代わりにメールを送信します。

はここに私のコードです:私はサインインして私のアプリの中に、私はAccountChooserダイアログで、それを選択することで、事前に設定されたアカウントを使用する場合はしようとすると、今

@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); 
    } 
} 

、それが完璧に動作します。

しかし、AccountChooserダイアログで、私の代わりに、ユーザー名のuserName = acct.getDisplayName()Account Chooserオプション「アカウントを追加」して、新しいアカウントを追加し、それは私に与えE-Mailを使用し、中に私はnullを与えた場合url = acct.getPhotoUrl()

私のアプリをログアウトして、同じアカウントをアカウントチューザーからSAMEアカウントを選択して使用すると、それは完全に機能し、理由がわかりません。

ご協力いただければ幸いです。

+0

これを解決しましたか、これがなぜ起こったのですか? –

+0

その前には運がありません。私がしたのは、Googleから名前やメールが届いているかどうかを確認することでした。メールを受け取ったら、アカウントの選択をもう一度開きました。 –

答えて

0

GoogleSignInOptionsのrequestIdToken( 'Your server client id')を追加した後も同じ問題が発生しました.Googleログイン後に新しいデータを追加するとすべてのデータが取得されます。更新されたGoogleSignInOptionsコードは、サーバーのクライアントIDをGoogleプロジェクトのOuthクライアントIDに置き換えると、適切なデータが得られます。

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

はまた、このGoogle Signin returns display name as null only when Add account in the flow

感謝を参照してください。

関連する問題