简体   繁体   中英

I can not get my Google sign-in to work via Firebase. The Google button is just unresponsive

The Google button just does nothing, I got no error or nothing. I need another eye to look at it.

    public class GoogleSignIn extends AppCompatActivity {

    private SignInButton googleSignInBtn;

    final String TAG ="Google-SignInActivity";

    private static final int RC_SIGN_IN = 10;

    private GoogleApiClient mGoogleApiClient;

    private FirebaseAuth mAuth;

    GoogleSignInClient mGoogleSignInClient;



    private  FirebaseAuth.AuthStateListener mAuthStateListener;

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

        /*mAuthStateListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if(firebaseAuth.getCurrentUser() != null){
                    Intent optionIntent = new Intent(GoogleSignIn.this, OptionActivity.class);
                    startActivity(optionIntent);
                }
            }
        };*/

        mAuth = FirebaseAuth.getInstance();


        googleSignInBtn = (SignInButton) findViewById(R.id.googleSignIn);

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

         //mGoogleSignInClient = Auth.GoogleSignInApi.getClient(this, gso);

        mGoogleApiClient = new GoogleApiClient.Builder(getApplication())
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(GoogleSignIn.this, "Google sign in Error", Toast.LENGTH_SHORT).show();

                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API,gso)
                .build();

        googleSignInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthStateListener);
    }

    private void signIn() {
       Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        //Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        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
                Log.w(TAG, "Google sign in failed");
                // ...
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount account) {

        Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            Intent optionIntent = new Intent(GoogleSignIn.this, OptionActivity.class);
                            startActivity(optionIntent);
                            finish();

                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            Snackbar.make(findViewById(R.id.googleSignIn), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
                            //updateUI(null);
                        }

                        // ...
                    }
                });
    }

}

My dependencies are below and i have used all sorts of dependencies, but nothing. I have used all version of firebase depencies

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.onyebuchboss.bossweatherbitcoinapp"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.google.firebase:firebase-database:15.0.0'
    compile 'com.google.firebase:firebase-core:15.0.0'
    compile 'com.google.firebase:firebase-auth:15.0.0'
    compile 'com.google.android.gms:play-services-auth:15.0.0'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'com.android.support:design:26.1.0'
}


apply plugin: 'com.google.gms.google-services'

Please find the source code for the XML below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/linearLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:background="@drawable/green"
    tools:context=".MainActivity"
    tools:ignore="ExtraText">

    <com.google.android.gms.common.SignInButton
        android:layout_width="351dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login_email"
        android:layout_alignEnd="@+id/login_email"
        android:layout_alignRight="@+id/login_email"
        android:layout_marginBottom="43dp"
        android:id="@+id/googleSignIn">

    </com.google.android.gms.common.SignInButton>


    <AutoCompleteTextView
        android:id="@+id/login_email"
        android:layout_width="354dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login_password"
        android:layout_alignLeft="@+id/login_password"
        android:layout_alignStart="@+id/login_password"
        android:layout_marginBottom="19dp"
        android:hint="@string/prompt_email"
        android:textColor="@android:color/white"
        tools:textColor="@android:color/white"
        tools:textColorHighlight="@android:color/white"/><![CDATA[

    tools:layout_editor_absoluteX="17dp"
        tools:textColor="@android:color/white"/>

    ]]>

    <EditText
        android:id="@+id/login_password"
        android:layout_width="351dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/login_sign_in_button"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="79dp"
        android:ems="10"
        android:hint="@string/prompt_password"
        android:inputType="textPassword"
        android:textColorHint="@android:color/white"/>

    <Button
        android:id="@+id/login_sign_in_button"
        android:layout_width="351dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/login_password"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/login_password"
        android:layout_marginBottom="83dp"
        android:text="@string/action_sign_in_short"
        tools:background="?attr/colorPrimary"
        tools:layout_editor_absoluteY="400dp"
        tools:textColor="@android:color/white"/>

    <Button
        android:id="@+id/login_register_button"
        android:layout_width="351dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/login_sign_in_button"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/login_sign_in_button"
        android:layout_marginBottom="15dp"
        android:text="@string/action_register"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        tools:background="@color/colorPrimary"/>

</RelativeLayout>

please add some extra Log messages to these places: your signIn() method, the body of if(result.isSuccess()) {} inside onActivityResult () method.

Then run your app and check if these Log methods have been called. If not called, the sign-in request was unsuccessful and vice versa.From here, we will figure out what the problem is and move on to the next step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM