Baseflow / flutter_cached_network_image

Download, cache and show images in a flutter app
https://baseflow.com
2.43k stars 654 forks source link

Authorization Bearer Access Token doesn't seem to work #820

Open WillComesBack opened 1 year ago

WillComesBack commented 1 year ago

I have a piece of code that passes in a bearer token

  Map<String, String>? get authorizationHeader =>
      { 'Authorization': 'Bearer $accessToken' };

I get the token and I make this call, but I get a 401 error

CachedNetworkImage(
    width: 48,
    height: 48,
    imageUrl: iconService.getRoomIconUrl(
      roomId: item.room.id,
      iconMetaData: item.room.iconMetaData,
    ),
    httpHeaders: iconService.authorizationHeader,
)

I do the same for Image.network and it works just fine. Am I doing something wrong?

Image.network(
    iconService.getRoomIconUrl(
      roomId: item.room.id,
      iconMetaData: item.room.iconMetaData,
    ),
    headers: iconService.authorizationHeader,
)
WillComesBack commented 1 year ago

I'm checking the console and I see it's not being passed image

but in Image.network, it is being passed just fine image

WillComesBack commented 1 year ago

Going to close. I realized the tag says it does not support Web as I was testing on Web. Sorry about that

Chralu commented 4 weeks ago

Hi,

I encounter the same issue running my project on Web platform.

Issue comes from the default image renderer for web : ImageRenderMethodForWeb.HtmlImage which ignores headers.

Workaround :

Using ImageRenderMethodForWeb.HttpGet fixes the issue :

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
      fit: BoxFit.cover,
      imageUrl:
          'http://pngimg.com/uploads/simpsons/simpsons_PNG94.png',
      httpHeaders: const {
        'test_header': 'test value',
      },
    );
  }
}
Joyker commented 2 weeks ago

Same issue, tested on android