代码之家  ›  专栏  ›  技术社区  ›  joshua

Firebase Google登录不起作用[复制]

  •  1
  • joshua  · 技术社区  · 7 年前

    按钮出现了,但由于某种原因,当我点击它时,它会抛出 ApiException 错误 onActivityResult 什么也不做。没有弹出窗口或提示。

    活动签名.java:

    package com.pineapple.edufinity.clubs;
    
    import android.content.Intent;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    
    import com.google.android.gms.auth.api.signin.GoogleSignIn;
    import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
    import com.google.android.gms.auth.api.signin.GoogleSignInClient;
    import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
    import com.google.android.gms.common.SignInButton;
    import com.google.android.gms.auth.api.Auth;
    import com.google.android.gms.common.api.ApiException;
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.auth.AuthCredential;
    import com.google.firebase.auth.AuthResult;
    import com.google.firebase.auth.FirebaseAuth;
    import com.google.firebase.auth.FirebaseUser;
    import com.google.firebase.auth.GoogleAuthProvider;
    
    public class Activity_SignIn extends AppCompatActivity implements View.OnClickListener{
    
        GoogleSignInClient mGoogleSignInClient;
        private static int RC_SIGN_IN = 100;
        private FirebaseAuth mAuth;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_signin);
    
            // Configure sign-in to request the user's ID, email address, and basic
            // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken("298792187574-rog3m9mrp179p24nononkl28e2v9oi4p.apps.googleusercontent.com")
                    .requestEmail()
                    .build();
            // Build a GoogleSignInClient with the options specified by gso.
            mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    
            // Set the dimensions of the sign-in button.
            SignInButton signInButton = findViewById(R.id.sign_in_button);
            signInButton.setSize(SignInButton.SIZE_STANDARD);
            signInButton.setOnClickListener(this);
    
            mAuth = FirebaseAuth.getInstance();
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sign_in_button:
                    signIn();
                    break;
                // ...
            }
        }
    
        private void signIn() {
            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) {
                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("FAIL", "Google sign in failed", e);
                    // ...
                }
            }
        }
    
        private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
            final String TAG = "SIGNIN";
            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) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d(TAG, "signInWithCredential:success");
                                FirebaseUser user = mAuth.getCurrentUser();
                                updateUI(user);
                            } else {
                                // If sign in fails, display a message to the user.
                                Log.w(TAG, "signInWithCredential:failure", task.getException());
                                //Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
                                updateUI(null);
                            }
    
                            // ...
                        }
                    });
        }
    
        @Override
        protected void onStart () {
            super.onStart();
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
            updateUI(currentUser);
        }
    
        private void updateUI(FirebaseUser user) {
            if(user != null){
                Intent startIntent = new Intent(getApplicationContext(), Activity_ClubsDashboard.class);
                startActivity(startIntent);
            }
        }
    }
    

    活动登录.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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activity_SignIn">
    
        <com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="140dp" />
    
    </RelativeLayout>
    

    build.gradle公司:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.pineapple.edufinity.clubs"
            minSdkVersion 24
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        //noinspection GradleCompatible
        implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
        implementation 'com.google.firebase:firebase-auth:16.0.2'
        implementation 'com.google.android.gms:play-services-auth:15.0.1'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    
    apply plugin: 'com.google.gms.google-services'
    

    Logcat错误:

    08-04 17:47:31.515 3417-3417/com.pineapple.edufinity.clubs W/FAIL: Google sign in failed
        com.google.android.gms.common.api.ApiException: 12500: 
            at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
            at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
            at com.pineapple.edufinity.clubs.Activity_SignIn.onActivityResult(Activity_SignIn.java:74)
            at android.app.Activity.dispatchActivityResult(Activity.java:6915)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
            at android.app.ActivityThread.-wrap20(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Alex Mamo    7 年前

    以下错误:

    Google sign in failed com.google.android.gms.common.api.ApiException: 12500
    

    通常发生在设备上的Google Play服务不是最新版本。要解决此问题,请更新到最新版本。如果您使用的是模拟器,请转到 Extended Controls Menu -> Google Play 并更新到最新版本。

        2
  •  1
  •   Ehsan Mashhadi    7 年前

    它是在模拟器上运行还是在真正的设备上运行?使用emulator和google play服务支持,将google play服务更新到最新版本并重新安装应用程序。