Delphi OAuth2 客户端
OAuth 2.0 / OAuth 2.1 客户端组件 — 支持带 PKCE 的授权码、客户端凭据、设备码、刷新令牌轮换及内置浏览器流。
OAuth 2.0 / OAuth 2.1 客户端组件 — 支持带 PKCE 的授权码、客户端凭据、设备码、刷新令牌轮换及内置浏览器流。
实现 OAuth 2.0/2.1 客户端侧 — 涵盖带 PKCE 的授权码、客户端凭据、设备码和刷新令牌流。包含用于重定向 URI 的小型嵌入式 HTTP 监听器。
TsgcHTTP_OAuth2_Client
Windows, macOS, Linux, iOS, Android
Standard / Professional / Enterprise
配置 ClientId / ClientSecret / Scope / 端点(或使用 Google / Microsoft / GitHub 的提供商预设),调用 Start,然后在 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();
实现带 PKCE、刷新令牌轮换和嵌入式回环重定向监听器的 OAuth 2.0 客户端接口。
授权码(含可选 PKCE,符合 RFC 7636)、客户端凭据(RFC 6749 §4.4)、设备码(RFC 8628)和刷新令牌(RFC 6749 §6)均作为类型化方法支持。
将 OAuth2Options.GrantType 设为 auth2CodePKCE,组件按 RFC 7636 生成 code_verifier / code_challenge 对并包含在请求中(OAuth 2.1 要求强制使用)。
兄弟组件 TsgcHTTP_OAuth2_Client_Google 和 TsgcHTTP_OAuth2_Client_Microsoft 已预填正确的端点和范围。其他提供商使用基类。
组件在配置的 RedirectURL 上启动一个小型嵌入式 HTTP 监听器,捕获授权码后关闭监听器 — 无需外部 Web 服务器。
授权后读取 CurrentRefreshToken 并由应用自行保存,下次启动时调用 Refresh,就不会再提示用户。
通过 Authentication.Token.OAuth 与 TsgcHTTPClient、TsgcHTTP2Client 或 TsgcWebSocketClient 配对 — Bearer 令牌自动注入每个请求。
本组件所实现标准的权威来源。