我无法使用Firebase Google登录方法登录

时间:2019-03-25 12:53:51

标签: android firebase google-signin

我正在尝试通过Firebase实现Google登录身份验证方法,但该方法无法正常工作,并且在运行应用程序时没有错误。 当我单击按钮时,它什么也没做。我试图更改我的代码,但结果相同。 我还使用了“ google-services.json”文件中的硬编码字符串,因为常见错误“无法解析(default_web_client_id)”

这是Android Studio“运行”标签中的调试结果。

enter image description here

GoogleLogin.class

public class google_login extends AppCompatActivity {

//a constant for detecting the login intent result
private static final int RC_SIGN_IN = 234;

//Tag for the logs optional
private static final String TAG = "autobus";

//creating a GoogleSignInClient object
GoogleSignInClient mGoogleSignInClient;

//And also a Firebase Auth object
FirebaseAuth mAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.relative_signup_activity);

    //first we intialized the FirebaseAuth object
    mAuth = FirebaseAuth.getInstance();

    //Then we need a GoogleSignInOptions object
    //And we need to build it as below
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("492413294880-ar3g4oe6bs8nhe02ilbsbh37mqltha3i.apps.googleusercontent.com")
            .requestEmail()
            .build();

    //Then we will get the GoogleSignInClient object from GoogleSignIn class
    mGoogleSignInClient = GoogleSignIn.getClient(google_login.this, gso);

    //Now we will attach a click listener to the sign_in_button
    //and inside onClick() method we are calling the signIn() method that will open
    //google sign in intent
    findViewById(R.id.glogin).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            signIn();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    //if the user is already signed in
    //we will close this activity
    //and take the user to profile activity
    if (mAuth.getCurrentUser() != null) {
        finish();
        startActivity(new Intent(google_login.this, relative_home.class));
    }
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //if the requestCode is the Google Sign In code that we defined at starting
    if (requestCode == RC_SIGN_IN) {

        //Getting the GoogleSignIn Task
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            //Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);

            //authenticating with firebase
            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            Toast.makeText(google_login.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    //getting the auth credential
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //Now using firebase we are signing in the user here
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();

                        Toast.makeText(google_login.this, "User Signed In", Toast.LENGTH_SHORT).show();
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(google_login.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}


//this method is called on click
private void signIn() {
    //getting the google signin intent
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();

    //starting the activity for result
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

}

LayoutFile

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    android:background="@drawable/bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/view"
    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:gravity="center_horizontal"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/usrimg"
            android:layout_width="90.0dip"
            android:layout_height="90.0dip"
            android:src="@drawable/add_user_image" />

        <TextView
            android:textSize="30.0sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10.0dip"
            android:text="DP" />
        <EditText android:textColor="#ffffffff"
            android:textColorHint="#ffffffff"
            android:gravity="center"
            android:id="@+id/name"
            android:background="@drawable/edit_text_style"
            android:layout_width="270.0dip"
            android:layout_height="50.0dip"
            android:hint="Username"
            android:inputType="text" />
        <EditText
            android:textColor="#ffffffff"
            android:textColorHint="#ffffffff"
            android:gravity="center"
            android:id="@+id/email"
            android:background="@drawable/edit_text_style"
            android:layout_width="270.0dip"
            android:layout_height="50.0dip"
            android:layout_marginTop="6.0dip"
            android:hint="Email"
            android:inputType="textEmailAddress" />

        <EditText
            android:textColor="#ffffffff"
            android:textColorHint="#ffffffff"
            android:gravity="center"
            android:id="@+id/password"
            android:background="@drawable/edit_text_style"
            android:layout_width="270.0dip"
            android:layout_height="50.0dip"
            android:layout_marginTop="6.0dip"
            android:hint="Password"
            android:inputType="textPassword" />

        <Button
            android:textSize="18.0sp"
            android:textColor="#ff000000"
            android:id="@+id/signup"
            android:background="@drawable/button_style_selector"
            android:layout_width="270.0dip"
            android:layout_height="45.0dip"
            android:layout_marginTop="20.0dip"
            android:text="Signup"
            android:textAllCaps="false" />

        <Button
            android:textSize="18.0sp"
            android:textColor="#ff000000"
            android:id="@+id/loginbtn"
            android:background="@drawable/button_style_selector"
            android:layout_width="270.0dip"
            android:layout_height="45.0dip"
            android:layout_marginTop="7.0dip"
            android:text="Login"
            android:textAllCaps="false" />
        <TextView
            android:textSize="15.0sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Or"
            android:textAllCaps="false" />

        <Button
            android:drawableStart="@drawable/ic_google_logo"
            android:background="@drawable/round_bg"
            android:textColor="#ff000000"
            android:id="@+id/glogin"
            android:layout_width="270.0dip"
            android:layout_height="50.0dip"
            android:layout_marginTop="4.0dip"
            android:text="@string/login_with_google"
             />
    </LinearLayout>
</LinearLayout>

屏幕截图:

enter image description here

0 个答案:

没有答案