Google使用Firebase Android登录 [英] Google Sign In with Firebase Android
本文介绍了Google使用Firebase Android登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class SignIn extends AppCompatActivity {
$ b $ private FirebaseAuth mAuth;
私有FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG =SignInActivity;
private static final int RC_SIGN_IN = 9001;
GoogleApiClient mGoogleApiClient;
私人静态按钮googleSignIn;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
//配置Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = firebaseAuth.getCurrentUser();
if(user!= null){
//用户登录
Log.d(TAG,onAuthStateChanged:signed_in:+ user.getUid());
} else {
//用户签出
Log.d(TAG,onAuthStateChanged:signed_out);
}
// ...
}
};
// ...
}
@Override
public void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
@Override
public void onStop(){
super.onStop();
if(mAuthListener!= null){
mAuth.removeAuthStateListener(mAuthListener);
private void signIn(){
意图signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN);
$ b @Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
//从GoogleSignInApi.getSignInIntent(...)启动Intent返回的结果;
if(requestCode == RC_SIGN_IN){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if(result.isSuccess()){
// Google登录成功后,使用Firebase进行身份验证
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(帐户);
} else {
// Google登录失败,正确更新用户界面
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct){
Log.d(TAG,firebaseAuthWithGoogle:+ acct.getId());
AuthCredential凭证= GoogleAuthProvider.getCredential(acct.getIdToken(),null);
mAuth.signInWithCredential(凭证)
.addOnCompleteListener(this,new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task){
Log.d(TAG,signInWithCredential:onComplete:+ task.isSuccessful());
//如果登录失败,向用户显示一条消息,如果登录成功
// auth状态侦听器将被通知,逻辑来处理
//登录用户可以在侦听器中处理
if(!task.isSuccessful()){
Log.w(TAG,signInWithCredential,task.getException());
Toast.makeText(SignIn.this,Authentication failed。,
Toast.LENGTH_SHORT).show();
}
// ...
}
});
$ b $ p $这是我的gradle的样子: compile'com.google.firebase:firebase-database:9.2.1'
compile'com.google.firebase:firebase -core:9.2.1'
compile'com.google.firebase:firebase-auth:9.2.1'
compile'com.google.android.gms:play-services-auth:9.2.1 '
解决方案这是我的代码:
保护无效signInwithGoogle(){
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString( R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(),this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
意图signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN);
$ b @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);
if(result.isSuccess()){
GoogleSignInAccount acct = result.getSignInAccount();
//使用
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
String personPhoneURL = acct.getPhotoUrl()。toString();
//用户用户=新用户();
// user.setUsername(personName);
// user.setEmail(personEmail);
// user.setPersonId(personId);
// user.setPersonProfileUrl(personPhoneURL);
// user.setSignedInWithGoogle(true);
// updateFirebaseData(user,personEmail);
$ b $ gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class,new UriSerializer())
.create();
// String userData = gson.toJson(user);
// EPreferenceManager.getSingleton()。setUserdata(getActivity(),userData);
firebaseAuthWithGoogle(acct);
} else {
Toast.makeText(getActivity(),登录时遇到问题请重试,Toast.LENGTH_SHORT).show();;
private void firebaseAuthWithGoogle(GoogleSignInAccount acct){
auth = FirebaseAuth.getInstance();
AuthCredential凭证= GoogleAuthProvider.getCredential(acct.getIdToken(),null);
auth.signInWithCredential(凭证)
.addOnCompleteListener(getActivity(),new OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@NonNull Task< AuthResult> task ){
//如果登录失败,向用户显示一条消息,如果登录成功
// auth状态侦听器将被通知,逻辑处理
if(!task.isSuccessful()){
Toast.makeText(getActivity(),Authentication failed。,
Toast。 LONGTH_SHORT).show();
} else {
Toast.makeText(getActivity(),Authentication pass。,
Toast.LENGTH_SHORT).show();
意图intent = new Intent(getActivity(),HomePageActiv) ity.class);
startActivity(intent);
getActivity()。finish();
}
}
});
}
I tried implementing Google sign in using firebase . I have gone through the documentation and watched a couple of videos but none of them worked. Whenever I click the Google Sign In button nothing happens. Also after successful login I want it to lead to the main activity. My app launches from Sign In activity.
public class SignIn extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
GoogleApiClient mGoogleApiClient;
private static Button googleSignIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
// ...
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
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) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed, update UI appropriately
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "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(TAG, "signInWithCredential", task.getException());
Toast.makeText(SignIn.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}
This is what my gradle looks like:
compile 'com.google.firebase:firebase-database:9.2.1'
compile 'com.google.firebase:firebase-core:9.2.1'
compile 'com.google.firebase:firebase-auth:9.2.1'
compile 'com.google.android.gms:play-services-auth:9.2.1'
解决方案 This is my code :
protected void signInwithGoogle(){
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
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);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
//TO USE
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
String personPhoneURL = acct.getPhotoUrl().toString();
// User user = new User();
// user.setUsername(personName);
// user.setEmail(personEmail);
// user.setPersonId(personId);
// user.setPersonProfileUrl(personPhoneURL);
// user.setSignedInWithGoogle(true);
// updateFirebaseData(user,personEmail);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Uri.class, new UriSerializer())
.create();
// String userData = gson.toJson(user);
// EPreferenceManager.getSingleton().setUserdata(getActivity(),userData);
firebaseAuthWithGoogle(acct);
} else {
Toast.makeText(getActivity(),"There was a trouble signing in-Please try again",Toast.LENGTH_SHORT).show();;
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
auth = FirebaseAuth.getInstance();
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
// 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()) {
Toast.makeText(getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), "Authentication pass.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), HomePageActivity.class);
startActivity(intent);
getActivity().finish();
}
}
});
}
这篇关于Google使用Firebase Android登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文