Client OAuth2 per Delphi
Componente client OAuth 2.0 / OAuth 2.1 — Authorization Code con PKCE, Client Credentials, Device Code, rotazione del refresh token e flusso browser integrato.
Componente client OAuth 2.0 / OAuth 2.1 — Authorization Code con PKCE, Client Credentials, Device Code, rotazione del refresh token e flusso browser integrato.
Implementa il lato client di OAuth 2.0 / 2.1 — copre i flussi Authorization Code (con PKCE), Client Credentials, Device Code e refresh-token. Include un piccolo listener HTTP integrato per il redirect URI.
TsgcHTTP_OAuth2_Client
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
Configura ClientId / ClientSecret / Scope / Endpoint (oppure usa un preset per Google / Microsoft / GitHub), chiama Start e usa Token nelle tue chiamate HTTP.
uses
sgcHTTP, sgcHTTP_OAuth_Types;
// Campo del form: 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';
// Apre il browser dell'utente all'URL di autorizzazione e ritorna subito
OAuth2.Start;
end;
// ... l'utente accede, il browser viene reindirizzato all'URL di callback
// ... il componente cattura il codice e lo scambia con i token
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 la superficie client di OAuth 2.0 con PKCE, rotazione dei refresh token e un listener di redirect su loopback integrato.
Authorization Code (con PKCE opzionale secondo RFC 7636), Client Credentials (RFC 6749 §4.4), Device Code (RFC 8628) e Refresh Token (RFC 6749 §6) sono tutti supportati come metodi tipizzati.
Imposta OAuth2Options.GrantType su auth2CodePKCE, il componente genera la coppia code_verifier / code_challenge e li include secondo la RFC 7636 (obbligatori in OAuth 2.1).
I componenti gemelli TsgcHTTP_OAuth2_Client_Google e TsgcHTTP_OAuth2_Client_Microsoft arrivano con gli endpoint e gli scope giusti già preimpostati. Per tutti gli altri usa la classe base.
Il componente avvia un piccolo listener HTTP integrato sul RedirectURL configurato, cattura il codice di autorizzazione e poi spegne il listener — non serve un web server esterno.
Leggi CurrentRefreshToken dopo l'autorizzazione e salvalo nella tua applicazione, poi chiama Refresh all'avvio successivo così l'utente non deve fare login di nuovo.
Abbinalo a TsgcHTTPClient, TsgcHTTP2Client o TsgcWebSocketClient tramite Authentication.Token.OAuth — il bearer token viene iniettato automaticamente in ogni richiesta.
Fonti autorevoli per gli standard implementati da questo componente.
Vai al riferimento del componente, scarica il progetto demo pronto all'uso e prova la versione trial.
| Guida online — TsgcHTTP_OAuth2_Client Riferimento completo di proprietà, metodi ed eventi per questo componente. | Apri | |
| Progetto demo — Demos\20.HTTP_Protocol\02.OAuth2_Authentication Progetto di esempio pronto all'uso. Incluso nel pacchetto sgcWebSockets — scarica la versione di prova qui sotto. | Apri | |
| Documento tecnico (PDF) Funzionalità, guida rapida, esempi di codice per Delphi e C++ Builder e riferimenti alle fonti primarie — solo per questo componente. | Apri | |
| Manuale utente (PDF) Manuale completo che copre tutti i componenti della libreria. | Apri |