gpt4 book ai didi

Android Google Sign-in with Firebase Authentication 登录不可靠

转载 作者:行者123 更新时间:2023-11-29 00:03:33 24 4
gpt4 key购买 nike

美好的一天

我目前正在开发一款需要谷歌登录的应用。我按照此处提供的 firebase 提供的说明进行操作:https://firebase.google.com/docs/auth/android/google-signin

但是,我从应用程序收到不可靠的登录信息。它运行良好了一段时间,然后开始出现问题。不幸的是,我什至没有太多的趋势可以描述。它主要发生在移动数据上,但有时甚至在 wifi 上也会发生。有时会登录,有时不会。除此之外,有时它会抛出一个 FirebaseNetworkError,有时它什么都不做。我应该提一下,我已经仔细检查过当它失败时总是有可靠的互联网连接(我总是检查 Youtube 视频并在 Chrome 上查找几页)。我还在不同移动和 wifi 网络上的大约 10 种不同的 android 设备上进行了测试。本质上,它有时会登录,有时不会。

我做了很多研究,但没有找到任何东西。如果我遗漏了什么,请告诉我。

编辑:用户登录后就没有问题了。所有其他 Firebase 服务都能完美运行。

这是我的身份验证 Activity (我有一个单独的身份验证,然后启动到我的 Main):

public class Authentication extends AppCompatActivity implements 
GoogleApiClient.OnConnectionFailedListener {

private FirebaseAuth mFirebaseAuth;
private GoogleApiClient apiClient;
private static final int RC_SIGN_IN = 9001;
SignInButton button;
Button signOut;
private static final String TAG = "GoogleActivity";



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

ActionBar actionBar = getSupportActionBar();
actionBar.hide();


button = (SignInButton) findViewById(R.id.googleSignIN);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();

}


});

signOut = (Button) findViewById(R.id.bSignOut);
signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});


// Set up Google Sign-in options
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();

// Set up Sign-In Api Client
apiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();


//mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseAuth = FirebaseAuth.getInstance();


}


@Override
protected void onStart() {
super.onStart();
// Check if current user is signed in
FirebaseUser currentUser = mFirebaseAuth.getCurrentUser();
startMain(currentUser);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

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();
firebaseAuthGoogle(account);
} else {

startMain(null);

}
}
}

private void firebaseAuthGoogle(GoogleSignInAccount account) {


AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);

mFirebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {

FirebaseUser user = mFirebaseAuth.getCurrentUser();
startMain(user);
} else {
Log.w(TAG, task.getException());
Toast.makeText(Authentication.this, "Authentication Failed. Please try again later", Toast.LENGTH_SHORT).show();
startMain(null);
}

}
});
}

private void signIn() {

Intent signIn = Auth.GoogleSignInApi.getSignInIntent(apiClient);
startActivityForResult(signIn, RC_SIGN_IN);

}

private void signOut() {
mFirebaseAuth.signOut();

Auth.GoogleSignInApi.signOut(apiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
startMain(null);
}

});
}

private void revokeAccess() {

mFirebaseAuth.signOut();

Auth.GoogleSignInApi.revokeAccess(apiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
startMain(null);
}
});
}

private void startMain(FirebaseUser currentUser) {
if (currentUser != null) {

Intent i = new Intent(Authentication.this, MainActivity.class);
startActivity(i);
finish();

}
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(Authentication.this, "Error", Toast.LENGTH_LONG).show();
}

这是网络错误的堆栈跟踪:

07-01 15:28:19.991 30844-30844/***.*********.******** W/GoogleActivity: 
com.google.firebase.FirebaseNetworkException: A network error (such as
timeout, interrupted connection or unreachable host) has occurred.
at
com.google.android.gms.internal.jz.zzK(Unknown Source)
at
com.google.android.gms.internal.jb.zza(Unknown Source)
at
com.google.android.gms.internal.kj.zzL(Unknown Source)
at
com.google.android.gms.internal.kl.onFailure(Unknown Source)
at
com.google.android.gms.internal.kb.onTransact(Unknown Source)
at
android.os.Binder.execTransact(Binder.java:446)

list 权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

项目 gradle 依赖项:

classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.0'

模块 gradle 依赖项(相关的):

compile 'com.firebaseui:firebase-ui:2.0.1'
compile 'com.google.firebase:firebase-database:11.0.2'
compile 'com.google.firebase:firebase-core:11.0.2'
compile 'com.google.firebase:firebase-auth:11.0.2'
compile 'com.google.android.gms:play-services-auth-base:11.0.2'
compile 'com.google.android.gms:play-services-auth:11.0.2'
compile 'com.google.android.gms:play-services-identity:11.0.2'
compile 'com.google.android.gms:play-services-location:11.0.2'
compile 'com.google.android.gms:play-services-maps:11.0.2'
compile 'com.google.android.gms:play-services-places:11.0.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-database:11.0.2'
compile 'com.google.firebase:firebase-messaging:11.0.2'

我还在我的 firebase 控制台中启用了 Google 登录,我输入了我的 SHA-1 key ,再次从 firebase 下载了 google-service.json 文件并生成了一个已签名的 apk 并对其进行了测试。

此时我感到非常沮丧,因为它有时有效,有时却无效,因此欢迎并非常感谢您提出任何建议。提前致谢!

最佳答案

我设法解决了这个问题。以下是对可能面临相同问题的任何人的建议:

  1. 在 android 监视器中,有一个方便的选项可以仅选择“firebase”日志(请原谅我的天真,我不知道这个)。就我而言,它表明没有网络连接。但是,这是因为 Google 服务在我设备上的移动数据上受到限制。

  2. 不过,该问题在其他设备上也仍然存在,因此我也生成了一个发布 SHA-1 key 并将其添加到我的 firebase 项目中。我下载了一个新的 google-services.json 文件并添加了它(我不知道这是否有必要,这只是一种预防措施)。当我无法弄清楚其他任何事情时,我已经在我的其他一些设备上测试了签名的 apk。因此,我认为这解决了其他设备上的登录问题。

关于Android Google Sign-in with Firebase Authentication 登录不可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com