Dan*_*jar 4 android firebase firebase-storage
我已经为我的应用设置了Firebase存储,并在应用和Firebase控制台上添加了用于匿名身份验证的代码。
它起初起作用,但是我不知道为什么它停止起作用,说用户没有访问该对象的权限
匿名身份验证已正确设置,我确实看到它可以正常工作,代码几乎就像Google Firebase文档一样
日志猫:
D / FirebaseAuth:signInAnonymously:onComplete:true
D / FirebaseAuth:onAuthStateChanged:signed_in:(随机身份验证用户ID)
...当我从Firebase请求项目时
E / StorageUtil:获取令牌java.util.concurrent.ExecutionException时出错:com.google.firebase.FirebaseException:发生内部错误。[遇到内部错误。] I / DpmTcmClient:来自com.android.okhttp.TcmIdleTimerMonitor的RegisterTcmMonitor W / NetworkRequest:没有针对请求E / StorageException的身份验证令牌:发生StorageException。用户无权访问此对象。代码:-13021 Http结果:403
有人可以帮忙吗?
声明变量
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
Run Code Online (Sandbox Code Playgroud)
在OnCreate方法上
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d("FirebaseAuth", "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d("FirebaseAuth", "onAuthStateChanged:signed_out");
}
// ...
}
};
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("FirebaseAuth", "signInAnonymously: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("FirebaseAuth", "signInAnonymously", task.getException());
Toast.makeText(SingleMemeEditor.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
Run Code Online (Sandbox Code Playgroud)
以及从存储中获取的方法:
Bitmap bmp;
final Context lContext = context; //getting the MainActivity Context
final String lFileName = fileName; //filename to download
final String lCatPath = catPath; //internal categorization folder
FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl(context.getResources().getString(R.string.firebase_bucket));
// Create a reference with an initial file path and name
StorageReference filesRef = storageRef.child("files/" + fileName);
try
{
final File localFile = File.createTempFile("images", "jpg");
filesRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>()
{
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot)
{
// Local temp file has been created
File file = new File(getDirectory(lContext)
+ File.separator + lCatPath + File.separator + lFileName);
try
{
Boolean b = file.createNewFile();
if(b)
{
FileInputStream in = new FileInputStream(localFile);
FileOutputStream out = new FileOutputStream(file);
// Transfer bytes from in to out
byte[] buf = new byte[(int)localFile.length()];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
Drawable.createFromPath(file.getPath())).getBitmap());
}
catch (IOException ex)
{
// Handle any errors
Log.e("CopyingFromTemp", ex.getMessage());
}
}
}).addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception ex)
{
// Handle any errors
Log.e("FirebaseDownloadError", ex.getMessage());
}
});
}
catch(Exception ex)
{
Log.e("FirebaseDownloadError", ex.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我也使用标准的安全规则:
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
Run Code Online (Sandbox Code Playgroud)
正如本杰明·沃夫(Benjamin Wulfe)所暗示的那样,我删除了手机上的应用程序数据,并且该数据正常运行,这意味着某种令牌数据已存储在手机上,并且匿名身份验证正在获取旧的会话数据。
所以我在signInAnonymously之前添加了退出代码
mAuth.signOut();
Run Code Online (Sandbox Code Playgroud)
并做了!
感谢大家的帮助!
编辑:我发现了另一种方法,该方法比注销然后再登录更好(这导致Firebase控制台上有数百个未使用的匿名用户,并且因为该应用程序尚未投入生产,因此将有数百万个)。
这就是我所做的:
if (mAuth.getCurrentUser() != null)
{
mAuth.getCurrentUser().reload();
}
else
{
mAuth.signInAnonymously()
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d("FirebaseAuth", "signInAnonymously: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("FirebaseAuth", "signInAnonymously", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
Run Code Online (Sandbox Code Playgroud)
这只会重新加载当前经过身份验证的(匿名)用户。
| 归档时间: |
|
| 查看次数: |
7311 次 |
| 最近记录: |