TsgcHTTP2Client | Autenticação de Cliente

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.

 

Basic Authentication

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;

Bearer Token

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;

Valor Bearer de Third-party

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 
 = true

TsgcHTTP2Client.Request.BearerToken = "< value of the token >"

 

OAuth2

Leia o artigo a seguir se quiser usar nosso componente OAuth2 com o cliente HTTP/2.