O cliente HTTP/2 suporta 2 tipos de autenticação: Basic Authentication e OAuth2 Authentication.
Utilize o evento OnHTTP2Authorization para tratar ambos os tipos de autenticação.
Se o servidor retornar um cabeçalho solicitando Basic Authentication, defina em OnHTTP2Authorization o nome de usuário e a senha.
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 o servidor retornar um cabeçalho requisitando autenticação Bearer Token, defina o token em 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 você já conhece o valor do Bearer, porque o obteve usando outro método, você pode passar o valor do Bearer como um cabeçalho HTTP usando as seguintes propriedades da requisição, basta defini-las antes de chamar qualquer método de requisição HTTP:
TsgcHTTP2Client.Request.BearerAuthentication = trueTsgcHTTP2Client.Request.BearerToken = "< value of the token >"
Leia o artigo a seguir se quiser usar nosso componente OAuth2 com o cliente HTTP/2.