簡體   English   中英

我無法通過Firebase來使用Google登錄。 Google按鈕無響應

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

Google按鈕什么也不做,我沒有錯誤或什么也沒有。 我需要另一只眼睛看它。

    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);
                        }

                        // ...
                    }
                });
    }

}

我的依賴項在下面,我使用了各種各樣的依賴項,但是什么也沒有。 我已經使用了所有版本的Firebase依賴

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'

請在下面找到XML的源代碼

<?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>

請在以下位置添加一些額外的Log消息:您的signIn()方法,onActivityResult()方法中if(result.isSuccess()){}的主體。

然后運行您的應用程序,並檢查是否已調用這些Log方法。 如果未調用,則登錄請求將失敗,反之亦然。從這里,我們將找出問題所在,然后繼續下一步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM