Cliente OAuth2 para Delphi
OAuth 2.0 / OAuth 2.1 client component — Authorization Code with PKCE, Client Credentials, Device Code, refresh-token rotation and built-in browser flow.
OAuth 2.0 / OAuth 2.1 client component — Authorization Code with PKCE, Client Credentials, Device Code, refresh-token rotation and built-in browser flow.
Implementa o lado cliente do OAuth 2.0 / 2.1 — cobre Authorization Code (com PKCE), Client Credentials, Device Code e fluxos de refresh-token. Inclui um pequeno listener HTTP embutido para o redirect URI.
TsgcHTTP_OAuth2_Client
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
Configure ClientId / ClientSecret / Scope / Endpoints (ou use um preset de provedor para Google / Microsoft / GitHub), chame Start e use Token nas suas chamadas HTTP.
uses
sgcHTTP, sgcHTTP_OAuth_Types;
// OAuth2 is a form field: OAuth2: TsgcHTTP_OAuth2_Client;
procedure TForm1.Authorize;
begin
OAuth2 := TsgcHTTP_OAuth2_Client.Create(nil);
OAuth2.OnAfterAccessToken := OnAfterAccessTokenEvent;
OAuth2.OAuth2Options.GrantType := auth2CodePKCE;
OAuth2.OAuth2Options.ClientId := 'your-client-id';
OAuth2.OAuth2Options.ClientSecret := 'your-client-secret';
OAuth2.AuthorizationServerOptions.AuthURL := 'https://auth.example.com/authorize';
OAuth2.AuthorizationServerOptions.TokenURL := 'https://auth.example.com/token';
OAuth2.AuthorizationServerOptions.Scope.Clear;
OAuth2.AuthorizationServerOptions.Scope.Add('profile');
OAuth2.AuthorizationServerOptions.Scope.Add('email');
OAuth2.LocalServerOptions.IP := '127.0.0.1';
OAuth2.LocalServerOptions.Port := 5555;
OAuth2.LocalServerOptions.RedirectURL := 'http://localhost:5555/callback';
// Spawns the user browser at the auth URL and returns immediately
OAuth2.Start;
end;
// ... the user logs in, the browser is redirected to the callback URL,
// ... the component captures the code and exchanges it for tokens
procedure TForm1.OnAfterAccessTokenEvent(Sender: TObject; const Access_Token,
Token_Type, Expires_In, Refresh_Token, Scope, RawParams: String;
var Handled: Boolean);
begin
ShowMessage(Access_Token);
end;
// uses: sgcHTTP, sgcHTTP_OAuth_Types
TsgcHTTP_OAuth2_Client *OAuth2 = new TsgcHTTP_OAuth2_Client(this);
OAuth2->OnAfterAccessToken = OnAfterAccessTokenEvent;
OAuth2->OAuth2Options->GrantType = auth2CodePKCE;
OAuth2->OAuth2Options->ClientId = "your-client-id";
OAuth2->OAuth2Options->ClientSecret = "your-client-secret";
OAuth2->AuthorizationServerOptions->AuthURL = "https://auth.example.com/authorize";
OAuth2->AuthorizationServerOptions->TokenURL = "https://auth.example.com/token";
OAuth2->AuthorizationServerOptions->Scope->Clear();
OAuth2->AuthorizationServerOptions->Scope->Add("profile");
OAuth2->AuthorizationServerOptions->Scope->Add("email");
OAuth2->LocalServerOptions->IP = "127.0.0.1";
OAuth2->LocalServerOptions->Port = 5555;
OAuth2->LocalServerOptions->RedirectURL = "http://localhost:5555/callback";
OAuth2->Start();
Implementa a interface cliente OAuth 2.0 com PKCE, rotação de refresh-token e um listener de redirect loopback embutido.
Authorization Code (com PKCE opcional conforme RFC 7636), Client Credentials (RFC 6749 §4.4), Device Code (RFC 8628) e Refresh Token (RFC 6749 §6) são suportados como métodos tipados.
Defina OAuth2Options.GrantType como auth2CodePKCE, o componente gera o par code_verifier / code_challenge e os inclui conforme RFC 7636 (obrigatório no OAuth 2.1).
Os componentes TsgcHTTP_OAuth2_Client_Google e TsgcHTTP_OAuth2_Client_Microsoft já vêm com endpoints e escopos corretos pré-configurados. Use a classe base para outros provedores.
O componente inicia um pequeno listener HTTP embutido na RedirectURL configurada, captura o código de autorização e encerra o listener — sem necessidade de servidor web externo.
Leia CurrentRefreshToken após a autorização e guarde-o na sua aplicação, depois chame Refresh no próximo início para que o usuário não precise se autenticar de novo.
Combine com TsgcHTTPClient, TsgcHTTP2Client ou TsgcWebSocketClient via Authentication.Token.OAuth — o bearer token é injetado automaticamente em cada requisição.
Fontes autoritárias para os padrões implementados por este componente.
Acesse a referência do componente, obtenha o projeto de demonstração pronto para executar e baixe a versão de avaliação.
| Ajuda Online — TsgcHTTP_OAuth2_Client Referência completa de propriedades, métodos e eventos deste componente. | Open | |
| Projeto de Demo — Demos\20.HTTP_Protocol\02.OAuth2_Authentication Projeto de exemplo pronto para executar. Incluído no pacote sgcWebSockets — baixe a versão de avaliação abaixo. | Open | |
| Documento Técnico (PDF) Recursos, início rápido, exemplos de código para Delphi & C++ Builder e referências de fonte primária — somente este componente. | Open | |
| Manual do Usuário (PDF) Manual abrangente cobrindo todos os componentes da biblioteca. | Open |