验证在FirebaseStorage Flutter中不起作用

时间:2019-10-12 08:20:20

标签: flutter firebase-authentication firebase-storage

我正在尝试将图片上传到Flutter应用程序中的Firebase存储中。 我收到的错误消息是该用户没有上传权限。在我的存储规则中,将权限设置为在auth != null时进行读写。 我可以使用下载网址从存储设备加载图像。另外,我打印出FirebaseAuth当前用户并进行设置。如果我将Firebase存储规则设置为允许所有内容(不使用Auth),那么它将起作用。

我需要在某处显式传递身份验证用户吗?验证由应用程序内的提供程序包处理。

错误:

E/StorageException(17300):  at com.google.firebase.storage.UploadTask.shouldContinue(com.google.firebase:firebase-storage@@17.0.0:309)
E/StorageException(17300):  at com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage@@17.0.0:226)
E/StorageException(17300):  at com.google.firebase.storage.StorageTask.lambda$getRunnable$7(com.google.firebase:firebase-storage@@17.0.0:1106)
E/StorageException(17300):  at com.google.firebase.storage.StorageTask$$Lambda$10.run(Unknown Source:2)
E/StorageException(17300):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/StorageException(17300):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/StorageException(17300):  at java.lang.Thread.run(Thread.java:764)
E/StorageException(17300): Caused by: java.io.IOException: {  "error": {    "code": 403,    "message": "Permission denied. Could not perform this operation"  }}
E/StorageException(17300):  at com.google.firebase.storage.network.NetworkRequest.parseResponse(com.google.firebase:firebase-storage@@17.0.0:455)
E/StorageException(17300):  at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(com.google.firebase:firebase-storage@@17.0.0:435)
E/StorageException(17300):  at com.google.firebase.storage.network.NetworkRequest.processResponseStream(com.google.firebase:firebase-storage@@17.0.0:426)
E/StorageException(17300):  at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@17.0.0:280)
E/StorageException(17300):  at com.google.firebase.storage.network.NetworkRequest.performRequest(com.google.firebase:firebase-storage@@17.0.0:294)
E/StorageException(17300):  at com.google.firebase.storage.UploadTask.send(com.google.firebase:firebase-storage@@17.0.0:470)
E/StorageException(17300):  at com.google.firebase.storage.UploadTask.uploadChunk(com.google.firebase:firebase-storage@@17.0.0:429)
E/StorageException(17300):  at com.google.firebase.storage.UploadTask.run(com.google.firebase:firebase-storage@@17.0.0:225)
E/StorageException(17300):  ... 5 more

代码:

Future<void> changeProfilePicture(File _image, User user) async {
    print(await FirebaseAuth.instance.currentUser());
    final StorageReference _storageReference =
        FirebaseStorage().ref().child(user.id);
    final StorageUploadTask _uploadTask = _storageReference.putFile(_image);
    StorageTaskSnapshot snap = await _uploadTask.onComplete;
    String downloadUrl = await snap.ref.getDownloadURL();
    print(downloadUrl);
    user.setProfilePictureURL(downloadUrl);

    Firestore.instance
        .collection('users')
        .document(user.id)
        .setData(user.toMap());
  }

Firebase存储规则;

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if auth != null;
    }
  }
}

依赖性:

dependencies:
  flutter:
    sdk: flutter
  camera: ^0.5.4+2
  path_provider: ^1.3.0
  path: ^1.6.4
  geolocator: ^5.1.3
  firebase_core: ^0.4.0+9
  firebase_auth: ^0.14.0+5
  cloud_firestore: ^0.12.9+5
  firebase_storage: ^3.0.6
  provider: ^3.1.0
  intl: ^0.16.0

根据Firebase错误页面,错误代码表示发件人ID与令牌ID不同。 我为相同的结果尝试了不同的用户帐户。

  

SENDER_ID_MISMATCH(HTTP错误代码= 403),已验证的发件人ID与注册令牌的发件人ID不同。

1 个答案:

答案 0 :(得分:0)

尝试更改

 final StorageReference _storageReference = FirebaseStorage().ref().child(user.id);

收件人

final StorageReference _storageReference = FirebaseStorage.instance.ref().child(user.id);