【发布时间】:2020-10-13 21:32:07
【问题描述】:
我想知道如何使用用户 ID 令牌作为 url 中的参数向 Firebase 存储发出授权请求。现在使用“request.auth!= null”的firebase规则,我收到403网络错误(无法加载视频:您无权访问所请求的资源)。这是我的 GET 请求网址:
https://firebasestorage.googleapis.com/v0/b/<bucket>/o/<folder_name>%2F<video_name>.mp4?alt=media&auth=eyJh...<ID TOKEN>...Ll2un8ng
-在没有 firebase 规则的情况下,我可以通过此请求 url https://firebasestorage.googleapis.com/v0/b/<bucket>/o/<folder_name>%2F<video_name>.mp4?alt=media 成功获取资产
-也尝试过 token=、token_id=、tokenId=
-不使用 firebase SDK 来获取文件的原因是我可以使用 flutter video_player (https://pub.dev/packages/video_player#-example-tab-) 包并将其与 firebase 中的文件一起使用,如果有更好的使用方法,我会提到这一点现在 Flutter Web 中的 video_player 库:
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
closedCaptionFile: _loadCaptions(),
);
[编辑] 似乎无法将身份验证作为查询参数传递。经过一番探索,我找到了一种可接受的方式,仍然可以将 video_player 与受保护的 firebase 资产一起使用(如果您不使用规则来保护它们,则可以直接使用 firebase url)。我将在这里发布一些一般步骤和一些示例代码:
使用Storage Firebase SDK包获取Uint8List,getDownloadURL给出的uri有正确的header auth,例如
import 'package:firebase/firebase.dart';
final url = await storagePath.getDownloadURL();
final response = await http.get(url);
if (response.statusCode == 200) {
return response.bodyBytes;
}
使用 Uint8List 缓冲区初始化一个 Blob 对象,然后您将使用该对象创建一个 ObjectURL,它基本上为您提供与文件 url 相同的界面,用作视频播放器的网络 url
final blob = html.Blob([data.buffer], 'video/mp4');
final videoUrl = html.Url.createObjectUrl(blob);
videoPlayerController = VideoPlayerController.network(
videoUrl)
..initialize().then((_) {...
就是这样。
【问题讨论】:
-
在 URL 中包含 auth 令牌或 id 令牌确实是个坏主意。我建议您通过
Authorization标头或 POST 正文发送 id 令牌。 -
视频播放器包不支持标头 (github.com/flutter/flutter/issues/16466),正在寻找其他选项。感谢您的意见。
-
Firebase 存储似乎不支持通过 URL 中的 GET 查询字符串进行授权。您必须以某种方式传递标头(可能使用其他包/库)cloud.google.com/storage/docs/authentication#apiauth(是的,Firebase 存储使用 Google Cloud 存储,因此此链接是有效参考)
标签: firebase flutter firebase-authentication flutter-web flutter-video-player