OAuth 2.0 客户端
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 的提供商预设),调用 StartAuthorization,然后在 HTTP 调用中使用令牌。
uses
sgcHTTP;
var
OAuth2: TsgcHTTP_OAuth2_Client;
begin
OAuth2 := TsgcHTTP_OAuth2_Client.Create(nil);
OAuth2.OAuth2Options.ClientId := 'your-client-id';
OAuth2.OAuth2Options.ClientSecret := 'your-client-secret';
OAuth2.OAuth2Options.Scope := 'profile email';
OAuth2.OAuth2Options.RedirectURL := 'http://localhost:5555/callback';
OAuth2.OAuth2Options.AuthorizationURL := 'https://auth.example.com/authorize';
OAuth2.OAuth2Options.TokenURL := 'https://auth.example.com/token';
OAuth2.OAuth2Options.PKCE := True;
// Spawns the user browser at the auth URL
OAuth2.StartAuthorization;
// ... user logs in, browser is redirected to the callback URL
// ... the component captures the code, exchanges for tokens
ShowMessage(OAuth2.AccessToken);
end;
// uses: sgcHTTP
TsgcHTTP_OAuth2_Client *OAuth2 = new TsgcHTTP_OAuth2_Client(this);
OAuth2->OAuth2Options->ClientId = "your-client-id";
OAuth2->OAuth2Options->ClientSecret = "your-client-secret";
OAuth2->OAuth2Options->Scope = "profile email";
OAuth2->OAuth2Options->RedirectURL = "http://localhost:5555/callback";
OAuth2->OAuth2Options->PKCE = true;
OAuth2->StartAuthorization();
实现带 PKCE、刷新令牌轮换和嵌入式回环重定向监听器的 OAuth 2.0 客户端接口。
授权码(含可选 PKCE,符合 RFC 7636)、客户端凭据(RFC 6749 §4.4)、设备码(RFC 8628)和刷新令牌(RFC 6749 §6)均作为类型化方法支持。
切换 OAuth2Options.PKCE — 组件按 RFC 7636 生成 code_verifier / code_challenge 对并包含在请求中(OAuth 2.1 要求强制使用)。
兄弟组件 TsgcHTTP_OAuth2_Client_Google 和 TsgcHTTP_OAuth2_Client_Microsoft 已预填正确的端点和范围。其他提供商使用基类。
组件在配置的 RedirectURL 上启动一个小型嵌入式 HTTP 监听器,捕获授权码后关闭监听器 — 无需外部 Web 服务器。
SaveToFile / LoadFromFile 持久化刷新令牌(如已配置则加密存储),避免每次启动应用时都提示用户。
通过 Authentication.Token.OAuth2 与 TsgcHTTPComponentClient、TsgcHTTP2Client 或 TsgcWebSocketClient 配对 — Bearer 令牌自动注入每个请求。
本组件所实现标准的权威来源。