我已经结帐了针对Android (link)的Firebase-Auth演示项目。 我使用google-services.json进行设置。它在模拟器上运行,在S3(4.3)/ S3(迷你)(4.2)和A3(5.0.1)上运行。但它在我的Note 4(6.0.1)上得到了一个auth错误。
我从谷歌播放服务获得一个令牌。但#firebaseAuthWithGoogle(final GoogleSignInAccount acct)
中的任务从未成功过。
任何人都可以证实吗?或者让某人有理想如何解决这个问题?
package de.dokate.dagobert.foodchecker.android.app.persistence.firebase;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.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.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
/**
* Created by Dagobert on 23.05.2016.
*/
public class FireLoginHelper implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = FireLoginHelper.class.getSimpleName();
private static final int RC_SIGN_IN = 12345;
private final AppCompatActivity activity;
private final FirebaseApplication application;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
public class FireLoginHelper implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = FireLoginHelper.class.getSimpleName();
private static final int RC_SIGN_IN = 12345;
private final AppCompatActivity activity;
private final FirebaseApplication application;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
public FireLoginHelper(final AppCompatActivity activity, FirebaseApplication application) {
this.activity = activity;
this.application = application;
}
public void onCreate() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(activity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.enableAutoManage(activity, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAuth = FirebaseAuth.getInstance();
signIn();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
application.userChanged();
}
};
}
public void onStart() {
mAuth.addAuthStateListener(mAuthListener);
}
public void onStop() {
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
final GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
final GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
//TODO Fehlerbehandlung verbesser
Log.e(TAG, "Google auth fail");
}
}
}
private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {
Log.e(TAG, "firebaseAuthWithGoogle() " + acct.getIdToken());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
//TODO Fehlerbehandlung verbessern
Toast.makeText(activity, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Log.e(TAG, "Auth complete");
}
}
});
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
activity.startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed() " + connectionResult.getErrorMessage());
}
}
我得到了例外:
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: com.google.firebase.FirebaseApiNotAvailableException: API: InternalFirebaseAuth.FIREBASE_AUTH_API is not available on this device.
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.internal.zznp$zzb.zzv(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.internal.zzoj$zzc.zzz(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.internal.zzoj$zzc.onConnectionFailed(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.internal.zzoj$zzd.zzh(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.common.internal.zzd$zzk.zzl(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.common.internal.zzd$zza.zzw(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.common.internal.zzd$zze.zztr(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at android.os.Looper.loop(Looper.java:158)
05-25 17:25:36.221 3434-3434/de.dokate.dagobert.foodchecker.android.app W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
尊重Dagobert
答案 0 :(得分:0)
原来你不是唯一一个。我有一个类似的问题,显然有人也在GitHub上抱怨它。