3

How to add a Authorization header when making a call to WMS services on mapBox Android SDK.

we have a satellite image service provider called Airbus. we want to make a call to their WMS restful API using mapBox Android SDK. the issue is Airbus requires us to pass a Authorization header with the key (token) when making the call for security seasons. and on mapBox SDK you can only pass the url source as a string and not the header.

please not we can make calls to other WMS services that doesn't require Authorization header

THE CODE

String airBus_source = "https://view.geoapi-airbusds.com/api/v1/map/imagery.wms?version=1.1.1&request=GetMap&service=WMS&WIDTH=256&HEIGHT=256&FORMAT=image/png&EPSG:3857&bbox={bbox-epsg-3857}";
            setHeader.builder.addHeader("Authorization",Airbus_key);
            setHeader.builder.url(airBus_source);
            setHeader.builder.tag(airBus_source.toLowerCase(MapboxConstants.MAPBOX_LOCALE));

            RasterSource airbus_image = new RasterSource("web-map-source",new TileSet("tileset",airBus_source),256);
            mapboxMap.addSource(airbus_image);

            RasterLayer webMapLayer = new RasterLayer("web-map-layer", "web-map-source");
            mapboxMap.addLayerBelow(webMapLayer, "aeroway-taxiway"); 

1 Answers1

3

I think I found a way to do it by replacing OkHttpClient. Mapbox library allows it with com.mapbox.mapboxsdk.module.http.HttpRequestUtil.

OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addNetworkInterceptor(chain -> {
        // check the request path if you need
        Request newRequest = chain.request()
            .newBuilder()
            .addHeader("Authorization", Airbus_key)
            .build();
        return chain.proceed(newRequest);
    })
    .build();
HttpRequestUtil.setOkHttpClient(okHttpClient);
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98