简体   繁体   English

从 flutter Z2567A5EC9705EB7AC2C984033E0618D 应用程序发送 firebase 存储授权为 url 参数

[英]Send firebase storage authorization as url parameter from a flutter web app

I would like to know how to make an authorized request to firebase storage using the user Id Token as a parameter in the url.我想知道如何使用用户 Id Token 作为 url 中的参数向 firebase 存储发出授权请求。 Right now with a firebase rule of 'request.auth:= null' I receive a 403 network error (Failed to load video. You do not have permission to access the requested resource): Here is my GET request url:现在使用“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 

-WITHOUT the firebase rule in place I'm able to successfully get the asset with this request url https://firebasestorage.googleapis.com/v0/b/<bucket>/o/<folder_name>%2F<video_name>.mp4?alt=media -WITHOUT the firebase rule in place I'm able to successfully get the asset with this request url https://firebasestorage.googleapis.com/v0/b/<bucket>/o/<folder_name>%2F<video_name>.mp4?alt=media

-also tried token=, token_id=, tokenId= -还尝试了 token=、token_id=、tokenId=

-the reason for not using the firebase SDK to fetch the file is so that I can use the flutter video_player ( https://pub.dev/packages/video_player#-example-tab- ) package and use this with files in firebase, I mention this in case theres a better way to use the video_player library in flutter web right now: -the reason for not using the firebase SDK to fetch the file is so that I can use the flutter video_player ( https://pub.dev/packages/video_player#-example-tab- ) package and use this with files in firebase,我提到这一点,以防现在有更好的方法来使用 flutter web 中的 video_player 库:

_controller = VideoPlayerController.network(
      'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
      closedCaptionFile: _loadCaptions(),
    );

[EDIT] It appears that it's not possible to pass the auth in as a query parameter. [编辑] 似乎无法将身份验证作为查询参数传递。 After some exploring, I found an acceptable way to still use the video_player with your firebase assets that are protected (If you're not using rules to protect them, you can directly use the firebase url).经过一番探索,我找到了一种可接受的方式,仍然可以将 video_player 与受保护的 firebase 资产一起使用(如果您不使用规则来保护它们,则可以直接使用 firebase url)。 I will post some general steps here and some sample code:我将在这里发布一些一般步骤和一些示例代码:

Use the Storage Firebase SDK package to get the Uint8List, the uri given by getDownloadURL has the correct header auth, for example Use the Storage Firebase SDK package to get the Uint8List, the uri given by getDownloadURL has the correct header auth, for example

import 'package:firebase/firebase.dart';
final url = await storagePath.getDownloadURL();
final response = await http.get(url);
if (response.statusCode == 200) {
  return response.bodyBytes;
}

use the Uint8List buffer to init a Blob object which you'll use to then create an ObjectURL which basically gives you the same interface as a file url to use as the network url for your video player使用 Uint8List 缓冲区初始化 Blob object,然后您将使用它创建一个 ObjectURL,它基本上为您提供与文件 url 相同的界面,用作视频播放器的网络 Z572D4E421E5E6B9BC11D815EEA

final blob = html.Blob([data.buffer], 'video/mp4');
final videoUrl = html.Url.createObjectUrl(blob);
videoPlayerController = VideoPlayerController.network(
          videoUrl)
        ..initialize().then((_) {...

That's it.而已。

Internally all this package does for flutter-web is create an HtmlElementView widget here for which it passes a VideoElement (ref here ) from the package dart:html with the provided URL which translates to a <Video> tag inside a shadow dom element in your web page. Internally all this package does for flutter-web is create an HtmlElementView widget here for which it passes a VideoElement (ref here ) from the package dart:html with the provided URL which translates to a <Video> tag inside a shadow dom element in your web 页面。 The error 403 could also mean you are trying to access it from a different origin.错误 403 也可能意味着您正在尝试从不同的来源访问它。

I would suggest following approach.我建议采用以下方法。

  1. Check your console for any CORS related errors.检查您的控制台是否有任何与 CORS 相关的错误。 If yes, then you will have to whitelist your ip/domain in the firebase storage.如果是,那么您必须在 firebase 存储中将您的 ip/域列入白名单。 Check this post for possible approach and more details here .在此处查看此帖子以了解可能的方法和更多详细信息。

  2. Check if you are able to access the URL directly with the authorization token as a query parameter as you suggested.检查您是否能够按照您的建议直接使用授权令牌作为查询参数访问 URL。 If not then, it is not the correct way to access the object and should be corrected.如果不是,那么这不是访问 object 的正确方法,应该更正。 You could update the question with the exact error details.您可以使用确切的错误详细信息更新问题。

Firebase Storage REST does not (rightly) support authorization from GET query string as you are trying to do. Firebase 存储 REST 不(正确地)支持来自 GET 查询字符串的授权,就像您尝试做的那样。 Instead, it uses the standard Authorization header (see here ).相反,它使用标准Authorization header(参见此处)。

Firebase cloud storage internally uses Google Cloud Storage. Firebase 云存储内部使用谷歌云存储。 Mentioned here 这里提到

If the library you use doesn't support HTTP headers yet, you must consider an alternative.如果您使用的库还不支持 HTTP 标头,您必须考虑替代方案。 The issue you mentioned in the comment shows that the feature is still under development, so you can also wait for the library to come out with the support for headers.您在评论中提到的问题表明该功能仍在开发中,因此您也可以等待库推出对标头的支持。

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

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