TsgcWebSocketHTTPServer를 사용하여 간단한 OAuth2 서버 예제를 만들어 보겠습니다.
먼저, 포트 443에서 수신하고 sgc.pem 파일의 자체 서명된 인증서를 사용하는 새 TsgcWebSocketHTTPServer를 생성하십시오.
oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.Port := 80;
oServer.SSLOptions.Port := 443;
oServer.SSLOptions.CertFile := 'sgc.pem';
oServer.SSLOptions.KeyFile := 'sgc.pem';
oServer.SSLOptions.RootCertFile := 'sgc.pem';
oServer.SSL := True;
그런 다음 TsgcHTTP_OAuth2_Server의 새 인스턴스를 생성하고 이전에 생성한 서버에 할당하십시오.
다음 값으로 새 Application을 등록하십시오:
Name: MyApp
RedirectURI: http://127.0.0.1:8080
ClientId: client-id
ClientSecret: client-secret
OAuth2 := TsgcHTTP_OAuth2_Server.Create(nil);
OAuth2.Apps.AddApp('MyApp', 'http://127.0.0.1:8080', 'client-id', 'client-secret');
oServer.Authentication.Enabled := True;
oServer.Authentication.OAuth.OAuth2 := OAuth2;
그런 다음 OAuth2 server 구성 요소의 OnOAuth2Authentication 이벤트를 처리하고 사용자를 로그인하는 자체 메서드를 구현하십시오. 로그인을 수락하기 위해 "user/secret" 쌍을 사용하겠습니다.
procedure OnAuth2Authentication(Connection: TsgcWSConnection; OAuth2: TsgcHTTPOAuth2Request; aUser,
aPassword: string; var Authenticated: Boolean);
begin
if ((aUser = 'user') and (aPassword = 'secret')) then
Authenticated := True;
end;
마지막으로 서버를 시작하고 OAuth2 클라이언트를 사용하여 로그인하십시오. 예를 들어 sgcWebSockets 라이브러리에 포함된 TsgcHTTP_OAuth2_Client를 사용할 수 있습니다.

새 Access Token을 요청하면 새 웹 브라우저 세션이 표시되며, 사용자는 연결을 허용한 다음 로그인해야 합니다.

로그인이 성공하면 새 토큰이 클라이언트에 반환됩니다. 그러면 모든 요청에 이 bearer 토큰이 HTTP 헤더에 포함되어야 합니다.
