gpt4 book ai didi

Android - Google 登录 getDisplayName() 给我电子邮件而不是新帐户上的显示名称添加

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:57 24 4
gpt4 key购买 nike

我正在尝试将 Google Sign-in 集成到我的 android 应用中。

这是我的代码:

@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 对话框中选择它来使用预配置的帐户,它会完美运行。

但是,如果在 AccountChooser 对话框中,我使用 Account Chooser“添加帐户” 选项并添加一个新帐户,它在 userName = acct.getDisplayName() 中给我电子邮件而不是用户名,在 url = acct.getPhotoUrl()

中给我 null

但是如果我注销我的应用程序并通过帐户选择器选择相同的帐户来使用相同的帐户,它会完美运行,但我无法弄清楚为什么。

我们将不胜感激。

最佳答案

我也遇到了同样的问题,在 GoogleSignInOptions 添加 requestIdToken('Your server client id') 现在我在添加新帐户时获取所有数据,而谷歌登录以下是更新的 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

谢谢。

关于Android - Google 登录 getDisplayName() 给我电子邮件而不是新帐户上的显示名称添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40863397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com