Il client HTTP/2 supporta 2 tipi di autenticazione: Autenticazione Basic e Autenticazione OAuth2.
Utilizzare l'evento OnHTTP2Authorization per gestire entrambi i tipi di autenticazione.
Se il server restituisce un'intestazione che richiede l'autenticazione Basic, impostare username e password in OnHTTP2Authorization.
oClient := TsgcHTTP2Client.Create(nil);
oClient.OnHTTP2Authorization := OnHTTP2AuthorizationEvent;
...
procedure OnHTTP2AuthorizationEvent(Sender: TObject; const Connection: TsgcHTTP2ConnectionClient; const AuthType, AuthData: string; var UserName, Password, aToken: string; var Handled: Boolean);
begin
if AuthType = 'Basic' then
begin
UserName := 'user';
Password := 'secret';
end;
end;
Se il server restituisce un'intestazione che richiede l'Autenticazione Bearer Token, impostare il token in OnHTTP2Authorization.
oClient := TsgcHTTP2Client.Create(nil);
oClient.OnHTTP2Authorization := OnHTTP2AuthorizationEvent;
...
procedure OnHTTP2AuthorizationEvent(Sender: TObject; const Connection: TsgcHTTP2ConnectionClient; const AuthType, AuthData: string; var UserName, Password, aToken: string; var Handled: Boolean);
begin
if AuthType = 'Bearer' then
begin
aToken := 'bearer token';
end;
end;
Se si conosce già il valore Bearer, perché è stato ottenuto con un altro metodo, è possibile passare il valore Bearer come intestazione HTTP usando le seguenti proprietà della richiesta, da impostare prima di chiamare qualsiasi metodo di richiesta HTTP:
TsgcHTTP2Client.Request.BearerAuthentication = trueTsgcHTTP2Client.Request.BearerToken = "< value of the token >"
Legga il seguente articolo se desidera utilizzare il nostro componente OAuth2 con il client HTTP/2.