简体   繁体   English

为什么我无法从 Firebase 存储中获取数据?

[英]Why I can't get data from Firebase Storage?

I need to get data from Firebase Storage and Firestore Database.我需要从 Firebase 存储和 Firestore 数据库获取数据。 I get text data from Firestore Database (links of images and songs in Firebase Storage), but I can't retrieve song and stream it in my app.我从 Firestore 数据库(Firebase 存储中的图像和歌曲的链接)获取文本数据,但我无法在我的应用程序中检索歌曲和 stream。 I get an error form logcat:我从 logcat 收到错误消息:

E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token. E/StorageUtil:获取令牌时出错 java.util.concurrent.ExecutionException:com.google.firebase.internal.api.FirebaseNoSignedInUserException:请在尝试获取令牌之前先登录。

But I don't sign in users in my app.但我不会在我的应用程序中登录用户。

logcat also writes an error: logcat也写错了:

E/StorageException: {  "error": {    "code": 404,    "message": "Not Found."  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found."  }}

logcat:日志:

2022-02-17 06:03:27.264 14195-14268/com.example.clearmind1 E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
2022-02-17 06:03:27.265 14195-14268/com.example.clearmind1 W/NetworkRequest: no auth token for request
2022-02-17 06:03:27.265 14195-14268/com.example.clearmind1 W/NetworkRequest: No App Check token for request.
2022-02-17 06:03:27.272 14195-14270/com.example.clearmind1 E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
2022-02-17 06:03:27.273 14195-14270/com.example.clearmind1 W/NetworkRequest: no auth token for request
2022-02-17 06:03:27.273 14195-14270/com.example.clearmind1 W/NetworkRequest: No App Check token for request.
2022-02-17 06:03:27.277 14195-14276/com.example.clearmind1 E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
2022-02-17 06:03:27.277 14195-14276/com.example.clearmind1 W/NetworkRequest: no auth token for request
2022-02-17 06:03:27.277 14195-14276/com.example.clearmind1 W/NetworkRequest: No App Check token for request.
2022-02-17 06:03:27.799 14195-14270/com.example.clearmind1 E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
2022-02-17 06:03:27.799 14195-14276/com.example.clearmind1 E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
2022-02-17 06:03:27.801 14195-14276/com.example.clearmind1 E/StorageException: {  "error": {    "code": 404,    "message": "Not Found."  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found."  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)
2022-02-17 06:03:27.801 14195-14264/com.example.clearmind1 E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
2022-02-17 06:03:27.802 14195-14264/com.example.clearmind1 E/StorageException: {  "error": {    "code": 404,    "message": "Not Found."  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found."  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)
2022-02-17 06:03:27.809 14195-14270/com.example.clearmind1 E/StorageException: {  "error": {    "code": 404,    "message": "Not Found."  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found."  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)
2022-02-17 06:03:27.955 14195-14268/com.example.clearmind1 E/StorageException: StorageException has occurred.
    Object does not exist at location.
     Code: -13010 HttpResult: 404
2022-02-17 06:03:27.955 14195-14268/com.example.clearmind1 E/StorageException: {  "error": {    "code": 404,    "message": "Not Found."  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found."  }}
        at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:445)
        at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:462)
        at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:453)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
        at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
        at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
        at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:77)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

Here I get text data from server:这里我从服务器获取文本数据:

ArrayList childItemList = new ArrayList<>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection(category).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            progressDialog.dismiss();
            for (QueryDocumentSnapshot document : task.getResult()) {
                Log.d(TAG, document.getId() + " => " + document.getData());
                ChildItem childItem = new ChildItem();
                childItem.setTrackTitle(document.getString("title"));
                childItem.setAuthor(document.getString("author"));
                childItem.setDuration(document.getString("duration"));
                childItem.setImage(document.getString("image"));
                childItem.setTrack(document.getString("track"));
                childItemList.add(childItem);
            }
        } else {
            Log.w(TAG, "Error getting documents.", task.getException());
            Toast.makeText(BrowseTracksActivity.this, task.getException() + "", Toast.LENGTH_SHORT);
        }
        childItemList.add(getDefaultChildItem());
        setAdapter();
    }
});

Here I'm trying to stream song (I'm sending link of the song through intent to another activity):在这里,我正在尝试 stream 歌曲(我通过意图将歌曲链接发送到另一个活动):

Intent intent=getIntent();
music= new MediaPlayer();
music.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
    String s=intent.getStringExtra("track");
    music.setDataSource(intent.getStringExtra("track"));
    music.prepare();
    music.start();

    Toast.makeText(this, "Audio started playing..", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    // this line of code is use to handle error while playing our audio file.
    Toast.makeText(this, "Error found is " + e, Toast.LENGTH_SHORT).show();
}

the error tells you that you need to sign in your users first.该错误告诉您需要先登录您的用户。 If you don't want to sign in your users, but want them to access the database, You can give them a token by signing them in anonymously.如果您不想让您的用户登录,但希望他们访问数据库,您可以通过匿名登录给他们一个令牌。 This way, you don't have to collect authentication data eg email, password, social login etc.这样,您就不必收集身份验证数据,例如 email、密码、社交登录等。

You can log in your user in the main activity without interrupting the flow.您可以在不中断流程的情况下在主要活动中登录您的用户。

Here is the code这是代码

mAuth.signInAnonymously()
    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                // Sign in success, User can now write and read the database.
                Log.d(TAG, "signInAnonymously:success");
                FirebaseUser user = mAuth.getCurrentUser();
                updateUI(user);
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInAnonymously:failure", task.getException());
                Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
                        Toast.LENGTH_SHORT).show();
                updateUI(null);
            }
        }
    });

Initialize FirebaseAuth in the onCreate method.在 onCreate 方法中初始化 FirebaseAuth。

Here is the firebase documentation Firebase anonymous log in Android这里是 firebase 文档Firebase 匿名登录 Android

Don't forget to change your database rules不要忘记更改数据库规则

"rules": {
".read": "auth != null",
".write": "auth != null"

} } } }

OR...或者...

You can allow all users to read and write your database (Not recommended), by just changing your rules to您可以允许所有用户读取和写入您的数据库(不推荐),只需将您的规则更改为

"rules": {
".read": "auth == null",
".write": "auth == null"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM