Android 在谷歌使用Firebase登录时检索用户性别,但不获取所有用户的性别?
场景: 我正在尝试使用android应用程序中的Android 在谷歌使用Firebase登录时检索用户性别,但不获取所有用户的性别?,android,google-signin,Android,Google Signin,场景: 我正在尝试使用android应用程序中的谷歌登录使用Firebase谷歌登录登录,我的基本需求是在用户登录我的应用程序时检索用户性别 问题: 即使我得到的是用户的性别,但不是所有的用户,那么问题出在哪里,因为只得到一些用户的性别是很奇怪的,所以我的问题是,为什么我在登录时没有得到所有用户的性别? public void performFirebaseLogin(GoogleSignInAccount acct, final Context context, final LoginSPre
谷歌登录
使用Firebase谷歌登录
登录,我的基本需求是在用户
登录我的应用程序时检索用户
性别
问题:
即使我得到的是用户的性别
,但不是所有的用户
,那么问题出在哪里,因为只得到一些用户的性别是很奇怪的,所以我的问题是,为什么我在登录时没有得到所有用户的性别?
public void performFirebaseLogin(GoogleSignInAccount acct, final Context context, final LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
final GoogleAccountCredential googleCredential = GoogleAccountCredential.usingOAuth2(
context, Collections.singleton(Scopes.PROFILE));
mAuth.signInWithCredential(credential)
.addOnCompleteListener((Activity) context, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
//here i am calling profile detail of user
new GetProfileDetails(user, googleCredential,loginSPrefRepositoryImpl).execute();
} else {
onLoginListener.onFailure("Login Failed");
}
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
}
});
}
//here i am retrieving user's information
class GetProfileDetails extends AsyncTask<Void, Void, Person> {
private PeopleService ps;
private int authError = -1;
private FirebaseUser user;
private LoginSPrefRepositoryImpl loginSPrefRepositoryImpl;
GetProfileDetails(FirebaseUser user, GoogleAccountCredential credential, LoginSPrefRepositoryImpl loginSPrefRepositoryImpl) {
this.loginSPrefRepositoryImpl = loginSPrefRepositoryImpl;
this.user = user;
credential.setSelectedAccount(
new Account(user.getEmail(), "com.google"));
HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
ps = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName("Google Sign In Quickstart")
.build();
}
@Override
protected Person doInBackground(Void... params) {
Person meProfile = null;
try {
meProfile = ps
.people()
.get("people/me")
.setPersonFields("genders")
.execute();
} catch (UserRecoverableAuthIOException e) {
e.printStackTrace();
authError = 0;
} catch (GoogleJsonResponseException e) {
e.printStackTrace();
authError = 1;
} catch (IOException e) {
e.printStackTrace();
authError = 2;
}
return meProfile;
}
@Override
protected void onPostExecute(Person meProfile) {
//mainAct.printBasic();
if (authError == 0) { //app has been revoke, re-authenticated required.
//mainAct.reqPerm();
} else if (authError == 1) {
Log.w(TAG, "People API might not enable at" +
" https://console.developers.google.com/apis/library/people.googleapis.com/?project=<project name>");
} else if (authError == 2) {
Log.w(TAG, "API io error");
} else {
if (meProfile != null) {
// Log.d("kkkk", "" + meProfile.getGenders());
JSONArray jsonArray = new JSONArray(meProfile.getGenders());
try {
JSONObject jsonObject = jsonArray.getJSONObject(0);
String gender = jsonObject.getString("formattedValue");
loginSPrefRepositoryImpl.isLoggedIn();
loginSPrefRepositoryImpl.setGender(gender);
onLoginListener.onSuccess(user);
} catch (JSONException e) {
e.printStackTrace();
onLoginListener.onFailure("Something went wrong !");
}
}
}
}
}
public void performFirebaseLogin(GoogleSignInAccount帐户、最终上下文上下文、最终loginspreprerepositoryimpl loginsprerepositoryimpl){
AuthCredential credential=GoogleAuthProvider.getCredential(acct.getIdToken(),null);
最终GoogleAccountCredential googleCredential=GoogleAccountCredential.usingAuth2(
上下文,Collections.singleton(Scopes.PROFILE));
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener((活动)上下文,新OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
FirebaseUser=mAuth.getCurrentUser();
如果(用户!=null){
//这里我调用用户的配置文件详细信息
新的GetProfileDetails(用户、googleCredential、loginSPrefRepositoryImpl).execute();
}否则{
onLoginListener.onFailure(“登录失败”);
}
}否则{
//如果登录失败,则向用户显示消息。
w(标记“signInWithCredential:failure”,task.getException());
}
}
});
}
//这里我正在检索用户的信息
类GetProfileDetails扩展了异步任务{
私人私人私人服务;
私有int authError=-1;
私有FirebaseUser用户;
私有LoginSprerepositoryImpl LoginSprerepositoryImpl;
GetProfileDetails(FirebaseUser用户、GoogleAccountCredential凭证、LoginSprerepositoryImpl LoginSprerepositoryImpl){
this.loginsprepreprepositoryimpl=loginsprepreprepositoryimpl;
this.user=用户;
credential.setSelectedAccount(
新帐户(user.getEmail(),“com.google”);
HttpTransport HTTP_TRANSPORT=AndroidHttp.newCompatibleTransport();
JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
ps=newPeopleService.Builder(HTTP_传输、JSON_工厂、凭证)
.setApplicationName(“谷歌登录快速启动”)
.build();
}
@凌驾
受保护人员背景(无效…参数){
Person-meProfile=null;
试一试{
meProfile=ps
.people()
.get(“人/我”)
.setPersonFields(“性别”)
.execute();
}捕获(UserRecoverableAuthIOE异常){
e、 printStackTrace();
authError=0;
}捕获(GoogleJsonResponseException e){
e、 printStackTrace();
authError=1;
}捕获(IOE异常){
e、 printStackTrace();
authError=2;
}
返回meProfile;
}
@凌驾
PostExecute上受保护的void(个人配置文件){
//mainAct.printBasic();
如果(authError==0){//app已被撤销,则需要重新验证。
//mainAct.reqPerm();
}else if(authError==1){
Log.w(标记“人员API可能无法在上启用”+
" https://console.developers.google.com/apis/library/people.googleapis.com/?project=");
}else if(authError==2){
Log.w(标记“API io错误”);
}否则{
if(meProfile!=null){
//Log.d(“kkk”,“meProfile.getGenders());
JSONArray JSONArray=newjsonarray(meProfile.getGenders());
试一试{
JSONObject JSONObject=jsonArray.getJSONObject(0);
字符串性别=jsonObject.getString(“formattedValue”);
loginsprefreepositoryimpl.isLoggedIn();
LoginsPreferRepositoryImpl.setGender(性别);
onLoginListener.onSuccess(用户);
}捕获(JSONException e){
e、 printStackTrace();
onLoginListener.onFailure(“出了点问题!”);
}
}
}
}
}
问题是,性别
应该由用户在其谷歌+
账户上明确分享。我建议你自己做实验
resourceName
=people/me
和personFields
=genders
resourceName
和etag
字段的响应性别可见性设置o