Google登录错误-Firebase android codelab

时间:2016-06-12 12:25:26

标签: android firebase google-signin

我已经尝试了很多次,重试并遵循Firebase Android Codelab中的确切步骤。我正在尝试使用Google帐户登录,但出于某种原因,它不成功。

SignInActivity.java

package com.google.firebase.codelab.friendlychat;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
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.GoogleAuthProvider;

public class SignInActivity extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
    private FirebaseAuth mFirebaseAuth;
    private static final String TAG = "SignInActivity";
    private static final int RC_SIGN_IN = 9001;

    private SignInButton mSignInButton;

    private GoogleApiClient mGoogleApiClient;

    // Firebase instance variables

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        // Initialize FirebaseAuth
        mFirebaseAuth = FirebaseAuth.getInstance();
        // Assign fields
        mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

        // Set click listeners
        mSignInButton.setOnClickListener(this);

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        // Initialize FirebaseAuth
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
        }
    }
    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
                Log.e(TAG, "Google Sign In failed." + result.toString());
            }
        }
    }
    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mFirebaseAuth.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(SignInActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            startActivity(new Intent(SignInActivity.this, MainActivity.class));
                            finish();
                        }
                    }
                });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
        Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
    }
}

添加了我的SHA1密钥。 谷歌登录结果是:com.google.android.gms.auth.api.signin.GoogleSignInResult@4211cac8

我以前遇到过这样的错误代码:Couldn't connect to Google API client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null} 但我很确定statusCode是不同的,因为我丢失了这个错误,我找不到它。如果有人能告诉我如何重做它以便我可以添加信息,那将是件好事。

Logcat(“错误”标记):(我确定这没用)

    06-12 22:07:09.353 5049-5064/com.google.firebase.codelab.friendlychat E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.google.firebase.codelab.friendlychat-6.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.firebase.codelab.friendlychat-6, /vendor/lib, /system/lib]]
06-12 22:07:09.363 5049-5064/com.google.firebase.codelab.friendlychat E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.google.firebase.codelab.friendlychat-6.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.firebase.codelab.friendlychat-6, /vendor/lib, /system/lib]]
06-12 22:07:09.543 5049-5049/com.google.firebase.codelab.friendlychat E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.crash.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.google.firebase.codelab.friendlychat-6.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.firebase.codelab.friendlychat-6, /vendor/lib, /system/lib]]
06-12 22:07:09.693 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.693 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.693 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.693 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.703 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.703 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.703 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.713 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.723 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.723 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:09.733 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.MainActivity.access$super
06-12 22:07:10.073 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.073 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.073 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.073 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.083 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.083 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.083 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.083 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.093 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.093 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:10.093 5049-5049/com.google.firebase.codelab.friendlychat E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.google.firebase.codelab.friendlychat.SignInActivity.access$super
06-12 22:07:20.113 5049-5064/com.google.firebase.codelab.friendlychat E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.google.firebase.codelab.friendlychat-6.apk"],nativeLibraryDirectories=[/data/app-lib/com.google.firebase.codelab.friendlychat-6, /vendor/lib, /system/lib]]
06-12 22:07:32.205 5049-5049/com.google.firebase.codelab.friendlychat E/SignInActivity: Google Sign In failed.com.google.android.gms.auth.api.signin.GoogleSignInResult@42106be8

3 个答案:

答案 0 :(得分:1)

原来我的Google-services.json出了问题,重新下载并解决了我的问题。

答案 1 :(得分:0)

我遇到了同样的错误。因此,重新下载并没有解决我的问题。它决定执行这些步骤。

Android Studio ,打开最右侧的 Gradle 菜单,从下拉菜单中打开 android-start (如果未显示任何内容,则刷新),打开任务,打开 android ,双击 signingReport 。参见this.

从此处获取SHA1密钥,然后创建一个新的Firebase项目,并在控制台中,单击“将Firebase添加到Android”后使用该SHA1密钥。 重新启动设备

对我有用。

答案 2 :(得分:0)

这是一个老问题,但我还是会回答,因为没有一个回复对我有用。

我在构建配置中遇到了完全相同的错误原因。确保您使用 import com.google.firebase.codelab.friendlychat.BuildConfig 而不是任何其他库,例如 firebase auth BuildConfig