TWebMultimediaPlayer URL / Headers

How to assign http headers (Authorization)
for TWebMultimediaPlayer file request
like this "WebClientConnection1.Headers.Add('Authorization=Bearer ' + Auth);" ?

Hi,

The HTML video tag does not support such functionality.

You could try to modify your backend to check the query parameters of your request URL and put the token in there. See a similar discussion here: https://stackoverflow.com/questions/52267065/how-to-make-a-html-5-video-tag-bearer-authenticated-with-a-webapi

Or alternatively you can use a TWebHTTPRequest component, set your request header and set the URL property to your video URL. This will most probably return your video in a binary format. After that, you'll need to encode the binary string to base64 and assign the result to the TWebMiltumediaPlayer.URL property.

Hi

that backend configuration looks fine, but how to do it with delphi sparkle / xdata server.

That second alternative I couldn't get to work.
I have a

  • function GetVideo(VideoId: integer; LangId: string): TStream;

which works correctly when correct http is assigned directly to WebMultimediaPlayer1.URL .
If I first call TWebHTTPRequest and assign that result to that URL , it does not play that video.

You can use a generic middleware to add custom code that converts a parameter in query string to an authentication header. Put the middleware before the JWT middleware and it should work. Something like this (not tested but you can get the idea):

procedure TForm6.XDataServer1GenericRequest(Sender: TObject; Context: THttpServerContext; Next: THttpServerProc);
var
  Params: TArray<TPair<string, string>>;
  Pair: TPair<string, string>;
begin
  if Context.Request.Uri.Query <> '' then
    if Context.Request.Headers.Get('Authorization') = '' then
    begin
      Params := TSparkleUtils.GetQueryParams(Context.Request.Uri.Query);
      for Pair in Params do
        if SameText(Pair.Key, 'access_token') then
        begin
          Context.Request.Headers.SetValue('Authorization', 'Bearer ' + Pair.Value);
          Break;
        end;
    end;
  Next(Context);
end;

Thanks it works fine.

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.