TsgcHTTP2Client | Client Authentication

HTTP/2 client supports 2 authentication types: Basic Authentication and OAuth2 Authentication.

 

Use OnHTTP2Authorization event to handle both types of authentication.

 

Basic Authentication

If server returns a header requesting Basic Authentication, set OnHTTP2Auithorization the username and password.

 


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

If server returns a header requesting Bearer Token Authentication, set OnHTTP2Authorization the token.

 


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;

Bearer value from Third-party

If you already know the Bearer Value, because you have obtained using another method, you can pass the Bearer value as an HTTP header using the following properties of the request, just set before calling any HTTP Request method:

 

 TsgcHTTP2Client.Request.BearerAuthentication 
 = true

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

 

OAuth2

Read the following article if you want to use our OAuth2 component with HTTP/2 client.