Flutter Firebase Storage: 请求缺少身份验证令牌

5

我一直在尝试使用Image_Picker上传图片到Firebase存储。当我想要将图片(imageFile)上传到Firebase存储时,

 Future uploadFile() async {
    StorageReference storageReference =
        storage.ref().child('profile/${Path.basename(imageFile.path)}}');

    print('uploading..');
    StorageUploadTask uploadTask = storageReference.putFile(imageFile);

    //waiting for the image to upload
    await uploadTask.onComplete;

    print('File Uploaded');
    storageReference.getDownloadURL().then((fileURL) {
      setState(() {
        imageURL = fileURL;
      });
      print(imageURL);
    });
  }

然而,在上传过程中,出现了一个错误,提到我没有授权令牌请求。我之前已经使用Firebase Auth将数据存储在数据库中,并且一切都已正确配置(我假设是这样的,因为Firebase给了我一个google.json文件)。

W/NetworkRequest( 5796): no auth token for request

E/StorageUtil( 5796): error getting token java.util.concurrent.TimeoutException: Timed out waiting for Task

我还尝试更改存储的规则,从如果auth != null则为读取,写入,改为只有读取和写入。

2个回答

1

检查你的Firebase存储规则。我认为默认选项是仅允许经过身份验证的用户。如果这是问题,只需将它们更改为最适合您需求的选项。


1
那正是我的问题,谢谢回答。 - Osama Abdullah
2
@AhmedKhattab,你设置了什么?能分享一下规则吗?拜托了,我也遇到了同样的问题! - Ali Yar Khan

1

您是否正在使用GoogleSignIn进行身份验证,并且能否提供Firebase用户身份验证代码?我遇到了相同的问题“没有授权令牌请求”,但通过使用以下代码解决了该问题。

final GoogleSignIn googleSignIn = GoogleSignIn();
FirebaseUser firebaseUser;
GoogleSignInAccount account = await googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await account.authentication;
final AuthCredential _cred = GoogleAuthProvider.getCredential(idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);
final AuthResult _res = await FirebaseAuth.instance.signInWithCredential(_cred);
firebaseUser = _res.user;

我认为这不仅适用于Google登录...而且适用于所有登录选项!!! - Ali Yar Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接