我知道如何将基本信息保存到我的 Firebase 数据库中,但我不知道如何将 google 登录集成保存到我的 Firebase 数据库中 有人可以解释一下吗?
这是我尝试保存信息的代码
DocumentReference documentReference = fStore.collection("users").document(personId);
Map<String,Object> user = new HashMap<>();
user.put("fName",personName);
user.put("email",personEmail);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ personId);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.toString());
}
});
}
} catch (ApiException e) {
}
最佳答案
Google 的此代码您必须在 google console 中注册您的应用程序|为谷歌注册。注册启用后 GMAIL API .
最后,您将从控制台获得一个特殊 key ,您必须在此处添加该 key .requestIdToken(getString(R.string.server_client_id))
GoogleSignInClient mGoogleSignInClient;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.server_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// call this on button call.
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 1);
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
if (requestCode == 1) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
if (account != null) {
String personName = account.getDisplayName();
String personEmail = account.getEmail();
String personId = account.getId();
}
} catch (ApiException e) {
}
}
关于android - 如何保存谷歌登录集成 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67297907/