Authentication persistence inside Windows Service

Hello Folks!

I do have an DataSnap application server that I recently migrated from an executable with a basic user interface to a Windows Service. Already in the past I had the problem that if the application server was running for a long time the TCloudCustomGoogle.TestTokens & TCloudCustomGoogle.RefreshAccess would return a False and I had to call TCloudGoogleWin.DoAuth.

I followed the example in TMS Cloud Pack Developers Guide on page 142 under "Authentication persistence". Here is my code:

procedure TsvmSyncGoogleCalendar.Authenticate;
var
  sTokens: string;
begin
  sTokens := AppServerSettings.GoogleTokens;
  if sTokens <> EmptyStr then
  begin

    GCalendar.TokensAsString := AppServerSettings.GoogleTokens;
    if not GCalendar.TestTokens then
      GCalendar.RefreshAccess;
    if not GCalendar.TestTokens then
    begin

      CoInitialize(nil);
      try
        GCalendar.DoAuth;
      finally
        CoUninitialize;
      end;
      AppServerSettings.GoogleTokens := GCalendar.TokensAsString;
    end;
  end
  else

  begin
    CoInitialize(nil);
    try
      GCalendar.DoAuth;
    finally
      CoUninitialize;
    end;
    AppServerSettings.GoogleTokens := GCalendar.TokensAsString;
  end;
end;


The CoInitialize was necessary as I used the standard browser Internet Explorer to answer the dialogueto accept changes to the Google Calendar.

Obviously in a Windows Service this is not an option. I wonder what I have overseen as there must be way to avoid these dialogues. What is the trick to stick with the current security token and avoid a renewal?

Posted the message too early.

Thanks for a short reply in advance.

Salut,
  Mathias

It is unusual that when you have a refresh token from Google, that a RefreshAccess call isn't giving you a new access token. Do you use the latest version of the components? If so, can you verify that in your persisted data, there is also a refresh token persisted?

Hello Bruno,

I think my problem is that I call GCalendar.TestTokens again and ignore the result of GCalendar.RefreshAccess. I think I can dramatically simplify the code for the Windows Service to ...

function TsvmSyncGoogleCalendar.Authenticate: Boolean;
begin
  Result := False;
  GCalendar.TokensAsString := AppServerSettings.GoogleTokens;
  if not GCalendar.TestTokens then
    Result := GCalendar.RefreshAccess;
end;

Turn the procedure into a function and return the result of RefreshAccess but never call GCalendar.DoAuth inside the Windows Service. If TsvmSyncGoogleCalendar.Authenticate returns false send a message to the server admin and ask for new tokens.

I will give that a go.

Salut,
  Mathias