'Flutter open Firebase Storage download url Permissions Denied 403

I am uploading PDF's in my app to Firebase Storage.

Now I want to open the PDF form my app in a browser. For that I tried using the url_launcher like this:

Future<void> _launchURL(
  BuildContext context, {
  required String url,
  String? errorMessage,
}) async {
  if (await canLaunch(url) == true) {
    await launch(Uri.encodeFull(url));
  }
}

But when opening the URL I get this:

{
  "error": {
    "code": 403,
    "message": "Permission denied."
  }
}

If setting my Storage Rules to allow: read, write; I get a 404-error. When changing my launch to:

Future<void> _launchURL(
  BuildContext context, {
  required String url,
  String? errorMessage,
}) async {
  if (await canLaunch(url) == true) {
    await launch(url);  // <- without encoding
  }
}

I get a Platform-Exeption:

Unhandled Exception: PlatformException(Error, Error while launching https://firebasestorage.googleapis.com/v0/b/appflug.appspot.com/o/studs%2FladSXGLivhRtVlJh3vogjXoxCdj1%2FlanguageTest%2FwIcon.pdf?alt=media&token=403f9631-58ed-4931-b76c-b9e6253e7d25, null, null)

but it is working on web!. And it is actually opening the PDF in my browser on my iOS device as well but it is throwing the exeption...

What am I missing? How can I open a downloadUrl from Firebase Storage in a browser on iOS, Android & Web?



Solution 1:[1]

Both of these methods worked for me using https://pub.dev/packages/url_launcher using version 6.1.0

Future<void> launchInWebViewWithoutJavaScript(String url) async {
  final Uri toLaunch = Uri.parse(url);
  if (!await launchUrl(
    toLaunch,
    mode: LaunchMode.inAppWebView,
    webViewConfiguration: const WebViewConfiguration(enableJavaScript: false),
  )) {
    throw 'Could not launch $url';
  }
}

and then alternatively you could use this:

Future<void> launchInWebViewOrVC(String url) async {
  final Uri toLaunch = Uri.parse(url);

  if (!await launchUrl(
    toLaunch,
    mode: LaunchMode.inAppWebView,
    webViewConfiguration: const WebViewConfiguration(
        headers: <String, String>{'my_header_key': 'my_header_value'}),
  )) {
    throw 'Could not launch $url';
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cigien