Facebook/Google在Android应用程序上注册Firebase
第一次开发Android应用程序。当登录FaceBook和Google时,我正在尝试在Firebase中注册用户 到目前为止,我已经用电子邮件和密码登录。 使用Facebook和Google登录 问题是:你是否必须在firebase中注册用户(当从google和facebook登录时)才能创建一个个人资料,例如图片 代码的这个范围是在我们项目的一个片段上实现的: onCreateViewFacebook/Google在Android应用程序上注册Firebase,android,facebook,firebase,google-authentication,Android,Facebook,Firebase,Google Authentication,第一次开发Android应用程序。当登录FaceBook和Google时,我正在尝试在Firebase中注册用户 到目前为止,我已经用电子邮件和密码登录。 使用Facebook和Google登录 问题是:你是否必须在firebase中注册用户(当从google和facebook登录时)才能创建一个个人资料,例如图片 代码的这个范围是在我们项目的一个片段上实现的: onCreateView GoogleSignInOptions gso = new GoogleSignInOptions.Buil
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.google_credential))
.requestEmail()
.build();
//
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
在onCreateView之外
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.google_credential))
.requestEmail()
.build();
//
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("MainACtivity", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("Main", "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("MainAcitivyt", "signInWithCredential", task.getException());
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
<> <强> >在Auth中对火炮标志进行编码时要考虑的事项 遵循Github回购协议中涵盖的流程,您可以查看回购协议中的一些工作示例活动 您还需要在Firebase控制台中启用Facebook和Google登录。另一个重要的一点是确保每次使用Firebase控制台进行更改时都使用最新的google-services.json文件 最后,确保在Firebase控制台中的Firebase项目中安装了所需的调试和发布SHA1指纹设置,以便在应用程序进入Play store时与Firebase服务连接 访问您的Firebase注册用户帐户
您还可以为用户提供注销和删除其登录帐户的功能,这也包含在文档和Github示例中。您不必手动向Firebase注册用户,它会自动存储该信息,您可以在Firebase控制台上的
身份验证
选项卡中看到该信息。在应用程序内部,您可以从FirebaseAuth.getInstance().getCurrentUser()
访问当前登录的用户。