我爲我的應用設置了firebase存儲,並在應用和firebase控制檯中添加了匿名認證的代碼。Firebase突然停止工作,使用匿名認證
它工作在第一,但我不知道爲什麼它停止工作,說用戶沒有權限訪問
匿名身份驗證設置正確的對象,我沒有看到它的工作,代碼幾乎是像谷歌文檔火力地堡
的logcat:
d/FirebaseAuth:signInAnonymously:的onComplete:真
d/FirebaseAuth: onAuthStateChanged:signed_in:(隨機auth用戶ID)
...當我請求火力
E/StorageUtil項目:錯誤得到令牌java.util.concurrent.ExecutionException:com.google.firebase.FirebaseException:一個內部錯誤。 [遇到內部錯誤。] I/DpmTcmClient:RegisterTcmMonitor from:com.android.okhttp.TcmIdleTimerMonitor W/NetworkRequest:no auth 請求標記E/StorageException:發生StorageException。 用戶沒有權限訪問此對象。 代碼:-13021 HttpResult:403
有人可以幫忙嗎?
聲明變量
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
在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();
}
// ...
}
});
,並從存儲獲得方法:
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());
}
也是我使用標準的安全規則:
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
顯示存儲的安全規則以及重現問題的最小代碼(此處的流程非常重要)。 –
編輯帖子以添加您請求的內容 –
不幸的是,代碼並未顯示如何確保用戶在上載圖像之前進行了身份驗證。在開始實際上傳之前,您可能需要添加一個簡單的檢查if(FirebaseAuth.getInstance()。getCurrentUser!0 null)'。 –