我正在使用firebase存储来上传文件,但是当我上传时我收到此错误
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzand: Please sign in before trying to get a token.
我用Google搜索但无法得到答案! 我已经登录了firebase。
答案 0 :(得分:13)
我认为您在上传文件之前没有签名。在启动器活动的onCreate()中,尝试此代码
FirebaseAuth mAuth = FirebaseAuth.getInstance();
然后在onStart(),
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
// do your stuff
} else {
signInAnonymously();
}
signInAnonymously()
private void signInAnonymously() {
mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// do your stuff
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.e(TAG, "signInAnonymously:FAILURE", exception);
}
});
}
这可以解决您的问题
答案 1 :(得分:4)
我遇到了同样的问题,这是因为默认情况下firebase只允许从已经过身份验证的用户上传文件。
在storage util异常之上,可能存在类似于此的日志:
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzajb: Please sign in before trying to get a token.
我们有自己的身份验证过程而不使用firebase,因此我们决定更改firebase控制台中的存储规则。
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
注意:更改规则将允许任何用户将文件上传到您的Firebase云服务器。
答案 2 :(得分:1)
以下步骤对我有用:
使用包ID从firebase生成正确的google-services.json
在build.gradle(app)
中pip3
确保在build.gradle(项目根文件夹)中添加以下依赖项
dependencies {
compile 'com.google.firebase:firebase-storage:10.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.firebase:firebase-client-android:2.4.0'
}
在活动中下载文件时,请添加以下代码:
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
}
答案 3 :(得分:1)
也许你只是不能“开始”到firebase。我说这是因为我创建了一个项目,通过Android Studio IDE连接到firebase存储,但我必须在控制台上手动“开始”。
答案 4 :(得分:1)
您必须先登录 FirebaseAuth。
您可以匿名登录,
但是如果您想登录用户的 google id,请尝试这样。
为了便于使用,我创建了一个管理器类。
object YourGoogleUserManager {
private lateinit var gso: GoogleSignInOptions
private lateinit var firebaseAuth: FirebaseAuth
fun init(context: Context) {
firebaseAuth = FirebaseAuth.getInstance()
gso = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(context.getString(R.string.default_web_client_id))
.build()
}
fun startGoogleSignInForResult(
fragment: Fragment,
onSuccess: (AuthResult) -> Unit,
onFail: (Exception) -> Unit
): ActivityResultLauncher<Intent> {
return fragment.registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
handleGoogleSignInResult(task, onSuccess, onFail)
}
}
fun signIn(
activityResultLauncher: ActivityResultLauncher<Intent>,
activity: Activity
) {
val googleSignInClient = GoogleSignIn.getClient(activity, gso)
activityResultLauncher.launch(googleSignInClient.signInIntent)
}
private fun handleGoogleSignInResult(
task: Task<GoogleSignInAccount>,
onSuccess: (AuthResult) -> Unit,
onFail: (Exception) -> Unit
) {
try {
val account = task.getResult(ApiException::class.java)
val credential = GoogleAuthProvider.getCredential(account?.idToken, null)
firebaseAuth.signInWithCredential(credential)
.addOnSuccessListener {
CatHolicLogger.log("success to firebase google sign in")
onSuccess(it)
}
.addOnFailureListener {
CatHolicLogger.log("fail to firebase google sign in")
onFail(it)
}
} catch (e: ApiException) {
CatHolicLogger.log("fail to firebase google sign in")
onFail(e)
}
}
fun signOut() {
firebaseAuth.signOut()
}
}
class YourApplication : Application() {
...
override fun onCreate() {
super.onCreate()
initGoogleUserManager()
}
...
private fun initGoogleUserManager() {
YourGoogleUserManager.init(this)
}
}
class YourFragment : Fragment() {
...
...
private val startGoogleSignInForResult =
YourGoogleUserManager.startGoogleSignInForResult(this, onSuccess = {
// your job after success to sign in
}, onFail = {
// your job after fail to sign in
})
...
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.signInButton.setOnClickListener {
YourGoogleUserManager.signIn(startGoogleSignInForResult, requireActivity())
}
}
}
答案 5 :(得分:0)
它可能就像互联网连接一样简单,即如果你的所有代码都是正确的,并且你已经在firebase上验证了所有用户。