Google Calendar | RefreshToken

Google Calendar API uses OAuth2 to authenticate against google servers, sgcWebSockets has a component which handles all the authentication process, but if your application closes and you attempts to connect again, you have 2 options:

 

1. Authenticate again using your Google APIs

2. Use the Refresh Token (if still valid), so you avoid the authentication process.

 

Using RefreshToken

The first time you Authenticate, use OnAuthToken event to save the RefreshToken if exists, you can save in an INIFile for example:

 


procedure OnGoogleCalendarAuthToken(Sender: TObject; const TokenType, Token, Data: string);
var
  oINI: TINIFile;
  oJSON: TsgcJSON;
begin
  oJSON := TsgcJSON.Create(nil);
  Try
    oJSON.Read(Data);
    if oJSON.Node['refresh_token']  nil then
    begin
      oINI := TINIFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
      Try
        oINI.WriteString('OAUTH2', 'Token', oJSON.Node['refresh_token'].Value);
      Finally
        oINI.Free;
      End;
    end;
  Finally
    oJSON.Free;
  End;
end;

Then when you start your application again, if there is a RefreshToken, call the method RefreshToken and pass the token as argument (previously you must set the Google Calendar API keys). If successful, you will login to google servers without re-authenticate again.

 


GoogleCalendar.RefreshToken('your refresh token here');