I have mostly vibe-coded a library to handle OAuth2 authentication to Microsoft Sharepoint, the unit and a sample are attached in case anyone of you finds it useful.
It works by creating a server to receive the authenticated token.
It works well, however the difficulty is in freeing the server, and the following code returns an error when I try to free the server Thread:
procedure TMS365Auth.LoginAuth;
var
...
Server: TFPHTTPServer;
Handler: TRequestHandler;
ServerThread: TServerThread;
begin
...
Server := TFPHTTPServer.Create(nil);
Handler := TRequestHandler.Create;
Server.OnRequest := @Handler.HandleRequest;
Server.Port := 8080;
ServerThread := TServerThread.Create(Server);
Sleep(250); // give the server time to bind
OpenBrowser(AuthURL);
//WriteLn('Waiting for authentication to complete...');
while (Handler.AuthCodeReceived = '') do
begin
Sleep(100);
end;
//The server is waiting to receive the Access Code.
if Handler.AuthCodeReceived <> '' then
begin
GetAccessToken(Handler.AuthCodeReceived);
FConnected := True;
end
else
begin
//WriteLn('Authentication failed or was cancelled.');
FConnected := False;
end;
Server.Active := False;
// this is the failure point!
//ServerThread.WaitFor;
//Server.Free;
//ServerThread.Free;
Handler.Free;
end;
I would be really grateful if someone could explain how to shutdown and free the server and serverthread without crashing the system.
ps. The system works well enough if the server isn't freed but of course that would not be ideal...
Thank you,
Ref